Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:09:53+00:00 2026-05-24T22:09:53+00:00

I found the need to use String.format in my project. However, this doesn’t work

  • 0

I found the need to use String.format in my project. However, this doesn’t work in GWT, and I have a replacement that uses GWT RegExp. However, I would like my project to run without errors when using the normal Swing interface that doesn’t depend on GWT. The shared code depends on the String.format though. The problem is that I don’t know how to make the GWT compiler use a different class than the other code. I already tried doing it all in one file, but it doesn’t work, since GWT can’t handle the NoClassDefFoundError exception that I use to detect when GWT is missing from classpath.

Here is my current code:

package testpackage.shared;

import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.regexp.shared.SplitResult;

public class Util {

  public static String format(final String format, final Object... args) {
    try {
      final RegExp regex = RegExp.compile("%[a-z]");
      final SplitResult split = regex.split(format);
      final StringBuffer msg = new StringBuffer();
      for (int pos = 0; pos < split.length() - 1; pos += 1) {
        msg.append(split.get(pos));
        msg.append(args[pos].toString());
      }
      msg.append(split.get(split.length() - 1));
      return msg.toString();
    } catch (NoClassDefFoundError ex)  {
      return String.format(format, args);
    }
  }
}

How do I make this compile and run on GWT and the JRE without GWT in classpath? Do I really need to make the Ant build script replace the whole file with a different one when building for GWT versus Swing/Sun JRE?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:09:53+00:00Added an answer on May 24, 2026 at 10:09 pm

    One solution is to use Deferred Binding Replacement:

    <replace-with class="testpackage.shared.UtilGWT">
      <when-type-is class="testpackage.shared.Util"/>
    </replace-with>
    

    And then call GWT.create(Util.class) in your GWT code. To avoid the Swing project picking up the UtilGWT class, I would put it into a different source folder (or maybe instruct the javac task of Ant to ignore the file).

    However, this still won’t allow you to reuse the part of the code which calls GWT.create() in your Swing application. So you’d have to create a factory, or call some initialization when the application starts, or something like that… all the usual problems if you’re living without Dependency Injection.

    If you want to use Dependency Injection (e.g. with Guice/Gin), the whole process of substituting an implementation becomes very natural: Create an abstract Util class/interface, and two implementations: UtilGWT and UtilJRE – then set up the correct instance in your module (you’d have a different module for your GWT and Swing applications), and you automatically get the correct implementation everywhere you need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found that in order to use PRISM's classes in XAML you need to
I am a windows dev, but I have recently found that I need to
I need an expression which matches,DateTime format (DD/MM/YYYY),i've already found it. However,it only works
I found all the code I need to make SHBrowseForFolder work in my application.
I found this jquery plugin which does exactly what I need, but I want
I need to send MMS thought a C# application. I have already found 2
I need to center a CSS menu that has an unknown width. I found
I have a long list of names that I need to have quotes around
I have a time in millis and I need to display that to the
I need to format a string to be double null-terminated string in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.