user1937709
user1937709

Reputation: 59

GWT - impossible to find working dir with Eclipse

I need to show on my panel the working dir.

I use String value = System.getProperty("user.dir"). Afterwards i put this string on label but I receive this message on console:

The method getProperty(String, String) in the type System is not applicable for the arguments (String).

I use eclipse.

Upvotes: 1

Views: 740

Answers (2)

Ajax
Ajax

Reputation: 2520

System.getProperty("key") is not supported, but System.getProperty("key", "default") IS supported, though it will only return the default value as there is not system properties per se.

If you need the working directory during gwt compile, you need to use a custom linker or generator, grab the system property at build time, and emit it as a public resource file.

For linkers, you have to export an external file that gwt can download and get the compile-time data you want. For generators, you just inject the string you want into compiled source.

Here's a slideshow on linkers that is actually very interesting.
http://dl.google.com/googleio/2010/gwt-gwt-linkers.pdf

If you don't want to use a linker and an extra http request, you can use a generator as well, which is likely much easier (and faster):

interface BuildData {
  String workingDirectory();
}
BuildData data = GWT.create(BuildData.class);
data.workingDirectory();

Then, you need to make a generator:

public class BuildDataGenerator extends IncrementalGenerator {
  @Override
 public RebindResult generateIncrementally(TreeLogger logger, 
     GeneratorContext context, String typeName){
  //generator boilerplate
  PrintWriter printWriter = context.tryCreate(logger, "com.foo", "BuildDataImpl");
  if (printWriter == null){
    logger.log(Type.TRACE, "Already generated");
    return new RebindResult(RebindMode.USE_PARTIAL_CACHED,"com.foo.BuildDataImpl");
  }
  SourceFileComposerFactory composer =
      new SourceFileComposerFactory("com.foo", "BuildDataImpl");
  //must implement interface we are generating to avoid class cast exception
  composer.addImplementedInterface("com.foo.BuildData");
  SourceWriter sw = composer.createSourceWriter(printWriter);
  //write the generated class; the class definition is done for you
  sw.println("public String workingDirectory(){");
  sw.println("return \""+System.getProperty("user.dir")+"\";");
  sw.println("}");
  return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING
       ,"com.foo.BuildDataImpl");

  }
}

Finally, you need to tell gwt to use your generator on your interface:

<generate-with class="dev.com.foo.BuildDataGenerator">
    <when-type-assignable class="com.foo.BuildData" />
</generate-with>

Upvotes: 1

appbootup
appbootup

Reputation: 9537

Issue

I am guessing you have not gone through GWT 101 - You cannot blindly use JAVA CODE on client side.

Explanation

You can find the list of classes and methods supported for GWT from JAVA. https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation

For System only the following are supported.

err, out,
System(), 
arraycopy(Object, int, Object, int, int), 
currentTimeMillis(), 
gc(), 
identityHashCode(Object), 
setErr(PrintStream),
setOut(PrintStream)

Solution

In your case Execute System.getProperty("user.dir") in your server side code and access it using RPC or any other server side gwt communication technique.

Upvotes: 6

Related Questions