views:

314

answers:

4

Hi,

I've been using Powershell-1.0 for command line needs for a while, instead of cmd.exe. Unfortunately, there are still some caveats when using java. I need to pass a property to a jar, like that:

java -jar -Duser.language=en any.jar

This line works fine in cmd.exe, but not in Powershell as it searches for another jar: Unable to access jarfile user.language=en

Using quotes doesn't help.

Is it doable in Powershell-1.0, or do I miss something in java?

Thanks in advance,

Regards,

jgran

+1  A: 

Try launching instead using the following pattern:

java -Duser.language=en -jar any.jar

That assumes that user.language is meant as a system property. If you meant it as a command line argument, change that to:

java -jar any.jar -Duser.language=en

I am actually surprised that the command line you mentioned works at all outside of powershell (though I have confirmed that it works fine for me too, even on Linux) and it is also a little strange that things would work differently inside and outside of powershell.

From java -help:

Usage: java [-options] class [args...]                 
           (to execute a class)                        
   or  java [-options] -jar jarfile [args...]          
           (to execute a jar file)                     
where options include:
...
    -D<name>=<value>
                  set a system property
...

So basically you should always put the JAR filename directly after the -jar command line option, and any JVM options (such as setting system properties with -D) before.

Adam Batkin
A: 

Thank you for the answer. But it didn't work out neither. If I put the property in proper way:

java -Duser.language=en any.jar

I get a NoClassDefFoundError:

PS > java -Duser.language=en -jar any.jar
Exception in thread "main" java.lang.NoClassDefFoundError: /language=en
Caused by: java.lang.ClassNotFoundException: .language=en
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: .language=en.  Program will exit.

On the other hand, if I pass the property after the jar, it's just ignored!

Regards,

jgran

jgran
Consider reading the Stack Overflow FAQ. This isn't a message board, so don't post followups as "answers". Edit your original question if necessary.
Adam Batkin
+2  A: 

Take a look at my answer to this question. Note how you can use echoargs.exe to diagnose these sort of problems. Most likely the fix would be to quote the parameter e.g.:

java -jar "-Duser.language=en" any.jar

You can test that using echoargs (from PowerShell Community Extensions):

echoargs -jar "-Duser.language=en" any.jar
Arg 0 is <-jar>
Arg 1 is <-Duser.language=en>
Arg 2 is <any.jar>
Keith Hill
A: 

Thanks so much Keith and Adam! That was it! Best regards jgran

jgran