Passing Java command-line arguments when using -cp - java

I'm writing a simple batch script to execute a game quicker (for a dev environment purpose), however I seem to be unable to pass the appropriate command-line arguments to the executable:
#ECHO OFF
set USERNAME=Testing
set MCDIR=%APPDATA%/.minecraft/
set GAMEDIR=%MCDIR%/versions/1.7.5/
set NATIVES=%GAMEDIR%/natives/
java -client -Xmx512M -Xms512M ^
-Djava.library.path=%NATIVES% ^
-cp %GAMEDIR%/1.7.5.jar:^
%MCDIR%/libraries/java3d/vecmath/1.3.1/vecmath-1.3.1.jar:^
%MCDIR%/libraries/net/sf/trove4j/trove4j/3.0.3/trove4j-3.0.3.jar:^
##etc... lots of libs
%MCDIR%/libraries/org/lwjgl/lwjgl/lwjgl_util/2.9.1/lwjgl_util-2.9.1.jar:^
%MCDIR%/libraries/org/lwjgl/lwjgl/lwjgl-platform/2.9.1/lwjgl-platform-2.9.1-natives-windows.jar:^
%MCDIR%/libraries/net/java/jinput/jinput-platform/2.0.5/jinput-platform-2.0.5-natives-windows.jar:^
net.minecraft.client.main.Main --username=%USERNAME% --version 1.7.5 --gameDir %MCDIR% --assetsDir %MCDIR%/assets --userProperties {} --accessToken Offline
pause
However this small script seems to fail the instant it goes beyond client.main.Main with any argument, effectively treating it as a JVM argument rather than a command-line program argument. Is there a different way to go about doing this when using -cp rather than -jar for calling a program in java? If not, what am I doing wrong?

It appears that there is no space between the end of your path (the last ^) and your Main class, so your Main class is effectively part of the -cp JVM argument. There is no class yet, so java still sees JVM args.
Try putting a space after the last part of the -cp value and your net.minecraft.client.main.Main, to separate the -cp value and your class on the command line.

Related

Run a Java Jar from Powershell

I checked quite a lot of other answers for this question on SO, but none of them really seem to work consistently, and correctly. Also, the examples weren't fitting my use case.
I have a java application jar which has a main class. I want to run it with the following arguments:
-Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
I am always getting simply the following when I try to run it using the following command on my powershell script (not command line)
Start-Process java -ArgumentList '-Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 the.jar.in.myClassPath.mainClass startArgs
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id SI ProcessName
------- ------ ----- ----- ----- ------ -- -- -----------
6 2 204 704 7 0.00 3588 1 java
My java home path is correctly set in environment variables so there's nothing wrong there.
I ran an echo of the argument set and they look correct. I am kind of stuck here to figure out why this causing a problem? Powershell is more complicated as opposed to DOS because when I run it in DOS Command Prompt, it just works. So what has gone wrong for me here?
Regards,
Why Start-Process? Just run the command, quoting the needed parameters. You should be able to just run it this way:
java -Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug "-Djava.compiler=NONE" "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
Notes:
Quote the the argument for -classpath since it contains ; characters.
Quote -D and its attached argument because it contains the . character.
Quote -X and its attached argument because it contains , and = characters.
Basically: Quote a parameter that contains characters that are otherwise syntactically meaningful to PowerShell.
You can troubleshoot executable parameter passing using a handy program I wrote called showargs.exe, which you can get from downloading the code associated with the following article:
IT Pro Today - Running Executables in PowerShell

Passing optional jvm arguments to java thru batch and shell scripts in command line

Got a java class with main method, say com.foo.Bar. But there are two optional vm(system) arguments for this class, say key1, key2.
Created a batch & shell shell scripts to call the above programs as below respectively.
Contents of Batch File(barRunner.cmd)
#echo off
set CLASSPATH=/bla/;/bla/
java -cp %CLASSPATH% com.foo.Bar %*
Contents of Shell script(barRunner.sh)
export CLASSPATH=/bla/:/bla/
java -cp $CLASSPATH com.foo.Bar $#
Now user calling in the below manner, but vm arguments cannot be read by Bar class
barRunner.cmd -Dkey1=value1
or
./barRunner.sh -Dkey2=value2 -Dkey1=value1
Suspect that the vm argument is passed after the class.
How to pass the vm arguments so that they are available before class name?
EDIT:
Have already tried changing script as below and it worked. But the issue if the class has program arguments.
java -cp %CLASSPATH% %* com.foo.Bar
Also aware of JAVA_OPTS, but it is a bit tedious for naive users; I mean, multiple commands to be run(set JAVA_OPTS in one command & and call the script in another line) and hesitant to use that way.
So thought of checking with forum if there is better way to achieve both vm & program arguments in single line and both args are optional.
VM arguments should be passed before the class name otherwise the arguments will become your program arguments so your command should be:
java -cp $CLASSPATH "$#" com.foo.Bar
With the above command, you could run with ./barRunner.sh -Dkey2=value2 -Dkey1=value1.
Now a slight issue is that all the arguments are passed as VM arguments. You may also want to pass some arguments to your program. To do that, you could do something like this:
java -cp $CLASSPATH $JVM_ARGS com.foo.Bar "$#"
With the above command, you could pass both JVM arguments and your program arguments like,
JVM_ARGS="-Dkey2=value2 -Dkey1=value1" ./barRunner.sh programArg1 programArg2

Translating windows bat file to linux shell script

This is my exact batch file. I have tried to convert it doing some research online and get an error
"Failed to execute child process "/home/pi/Desktop/TeachVal/TeachValLinuxShell" (No such file or directory)
echo off
cls
echo Running TeachVAL II...
set path=%path%;/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
cls
exit
This one is my attempt at translating.
#!/bin/bash
set +v
clear
echo "Running TeachVAL II..."
java -cp ".dir1;dir2;path/home/pi/Desktop/TeachVAL/comm.jar;
path/home/pi/Desktop/TeachVAL/Robot.jar;/home/pi/Desktop/TeachVAL/TeachVAL"
clear
exit
Welcome to Linux--life is good here, but there are a few things that work slightly differently, when compared to Windows.
One difference is that Windows uses semicolon (;) to separate entries in a list of paths, but Linux uses colons (:) for that purpose.
So, the Windows command:
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
would correspond to this on Linux:
java -classpath comm.jar:Robot.jar:TeachVAL TeachVAL
In general, on Linux, semicolons are used to put multiple command lines into a single line. Once you've learned that, I think you can then understand why:
java -cp .dir1;/home/pi/Desktop/TeachVAL/TeachVAL
would be the same as:
java -cp .dir1
/home/pi/Desktop/TeachVAL/TeachVAL
That would run java (with no class to be executed) and then try to run "/home/pi/Desktop/TeachVAL/TeachVAL" which can't be found.
There are many more differences to learn; here's a page that will help you get started: http://tldp.org/LDP/abs/html/dosbatch.html

Understand cmd command which runs java application

I have the following code in the batch:
"C:/EDI_Module/jre1.8.0_121/bin/java" -Xms2g -Xmx10g -cp .;C:/EDI_Module/jar/;C:/EDI_Module/lib/; com/solverelogistics/edi/MainGenerator
Can you please tell me what is going on here? I know it runs some app. Which one is the java application?
The JRE executable (the bytecode interpreter):
"C:/EDI_Module/jre1.8.0_121/bin/java"
The starting memory size:
-Xms2g
The maximum memory size:
-Xmx10g
The classpath:
-cp .;C:/EDI_Module/jar/;C:/EDI_Module/lib/;
The class containing a public static void main method to execute:
com/solverelogistics/edi/MainGenerator
Note: I would have typically expect the class to look something like this com.solverelogistics.edi.MainGenerator, with ., not /.

Error: could not find or load main class (another one)

I've searched throughout this site and tried a few solutions when receiving this message but nothing seems to work.
I am trying to invoke a shell script on Ubuntu 12.04.2 (with java-7-openjdk-amd64) that runs a java program and then I get a "Error: Could not find or load main class com.xx" error.
This is how my script invokes Java:
"$JAVA" $server_jvmargs $javaProps -Dxx.home="$XX_HOME" -Duser.dir="$XX_HOME" -cp $client_classpath $mainclass $args
And the arguments you see above are defined as follows:
args=$*
javaProps=
mainclass=com.xx
server_jvmargs="-Djava.awt.headless=true -Xms1024m -Xmx1024m $jvmargs"
XX_HOME="`pwd`/../.."
client_classpath="$XX_HOME/lib/client/patch.jar;$XX_HOME/lib/client/xyx-xxx.jar;$clientlibs;$XX_HOME/lib/server/standard-1.1.2.jar;$publictilesource;$respath;$XX_HOME/lib/client/xxmainclass.jar"
The mainclass variable is in the classpath located in the xxmainclass.jar file so I'm not sure as to why it cannot find it?
Does anyone have any ideas on what could be going on?
To see what actually happens when you run your script, invoke it with bash -x, or put set -x at the top; this will print each command before it's run, so you can see how it's actually starting the JVM. Without this information, it's hard to come up with a better diagnosis. That said...
You've been copying off Tomcat's startup scripts, it looks like. Don't; they're awful.
Something a little more correct on the shell side might look like this:
args=( "$#" )
javaProps=( )
mainclass=com.xx
server_jvmargs=( -Djava.awt.headless=true -Xms1024m -Xmx1024m "${jvmargs[#]}" )
XX_HOME="$PWD/../.."
client_classpath="$XX_HOME/lib/client/patch.jar:$XX_HOME/lib/client/xyx-xxx.jar:$clientlibs:$XX_HOME/lib/server/standard-1.1.2.jar:$publictilesource:$respath:$XX_HOME/lib/client/xxmainclass.jar"
java \
"${server_jvmargs[#]}" \
"${javaProps[#]}" \
-Dxx.home="$XX_HOME" \
-Duser.dir="$XX_HOME" \
-cp "$client_classpath" \
"$mainclass" "${args[#]}"
The use of ${foo[#]} expands the array foo with literal contents. Note that foo must be created as an array in this case, and you need to be using a shell that supports arrays (so your script needs to start with #!/bin/bash, not #!/bin/sh).
See http://mywiki.wooledge.org/BashFAQ/005 for an introduction to arrays in bash.
use a : instead of a ; in your classpath.
unix just rolls that way.
Try this:
Java -jar pathToYOurFile.jar
Please check if line end character is OS specific in your shell script

Categories

Resources