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?
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>
Using quotes works fine for me in PowerShell on Windows 7.
java "-Dmy.property=value" -jar myjar.jar
Be careful: the jar name must be placed right after -jar, and arguments placed after -jar myjar.jar will be passed to the program inside the jarFile.
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.
Related
I want to use an environment variable as a JVM option when executing java -jar.
The command I want to execute it:
java -XX:onOutOfMemory='echo test' -jar foo.jar
When I run the above command as is, the jar will run.
(If you don't have the foo.jar, you will get an Error: Unable to access jarfile foo.jar error. But this still means that the option gets used correctly).
But when I create an environment variable containing the JVM option, and run the command using that variable.
OOM="-XX:onOutOfMemory='echo test'"
java $OOM -jar foo.jar
Than I get the following error:
Error: Could not find or load main class test'
It seems like the java command is ignoring the quotes around 'echo test'.
After looking for similar questions on SO and on other websites, I tried various variations of using quotes:
OOM="-XX:OnOutOfMemoryError=\"echo test\""
OOM='-XX:OnOutOfMemoryError="echo test"'
OOM=-XX:OnOutOfMemoryError="echo test"
But they all result in the same error.
An article from Oracle concerning JVM options, mentions using a semicolon:
-XX:OnOutOfMemoryError="<cmd args>; <cmd args>"
But the purpose of the semicolon is to separate multiple commands, not command and arguments. So this does not fix my problem.
Does anybody know how I can correctly move the -XX:onOutOfMemory='echo test' option into an environment variable?
When running java, you should quote $OOM
Example:
java "$OOM" -jar foo.jar
See Why does my shell script choke on whitespace or other special characters? on Unix stackexchange for why this is needed.
I have a makefile that calls a java parser to process some files before compilation. While in linux works perfectly when I am running that on a Windows 7 machine using cygwin my input are recognized as jar files.
My command inside my make is something like
java -jar ${MY_DIR}/myParser.jar -arg1 -arg2 -arg3 input.file output.file ;\
and I get an error that input.file is not a valid jar file... I assume it has something to do with my paths and how cygwin handles java but I can't make it work.
Thanks in advance
Try putting up ()
exec java -jar $(cygpath -w /path/to/myParser.jar) -arg1 -arg2 -arg3 input.file output.file
Srry not enough rep to add comment .
I have to get some kinks out of a shell script for work, and one of the line looks like this:
-cp: this is the classpath
This is the set of classes that are used when running a specific class.
In your example; OrganT.Tune.Mix OrganT must be a class in the classpath (in this case, inside the OrganT.jar
Read the documentation, can be found here
Just a hint - under linux and mac you can use the
man <command goes here>
comman in the terminal/shell to display all parameters and usage information available for the specific command.
-cp stands for classpath. The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.
java -classpath .;YourJarFile.jar
I think you want to run a script for including the class path and execute the jar.
To do this in any text editor type java -jar YourJarFile.jar and save it, with extention (anyName.sh) assuming you have got linux flavour. Make it executable using the command chmod 775 anyName.sh
For windows type java -jar YourJarFile.jar, and save it with extention (anyName.bat)
java -server -Xmx2G -cp config:./* l2p.loginserver.LoginServer
MAC: OK!
WINDOWS: Cannot find class l2p.loginserver.LoginServer
LINUX: Cannot find class l2p.loginserver.LoginServer
Aditional info: jar file is called kernel.jar and it's in the same folder where the command is executed
if i use java -server -cp kernel.jar l2p.loginserver.LoginServer the class is started to load but i need config because i have log4j xml there.
Thanks!
See this answer
In java classpath, if you use wildcard *, it only loads the .jar files from that location.
java -server -Xmx2G -cp config:kernel.jar l2p.loginserver.LoginServer
Problem with log4j is that the first log4j.xml in the classpath will be loaded. So put the config in front.
I don't think you can use globbing like that in a -cp argument. They won't be expanded in the right way, colon-separated as you need them. Try
java -cp config:kernel.jar l2p.loginserver.LoginServer
(With the other arguments you need, of course.)
Note that this is assuming you're on Unix. On Windows you'd need
java -cp config;kernel.jar l2p.loginserver.LoginServer
(The path separator is ; on Windows, but : on Unix.)
In a bash shell script I tried these two versions:
java -jar abc.jar&
and
CMD="java -jar abc.jar&"
$CMD
The first verison works, and the second version complains that abc.jar cannot be found. Why?
Commands do run from current directory in a shell script.
This is why the first command in your test script worked.
The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.
Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.
What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.
Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.
java -jar /path/to/abc.jar&