How can I pass java -jar arguments in a batch file? - java

How can I pass java -jar arguments in a batch file ?
Code below doesn't work:
set classpath = c:/users/abc/desktop/project/trunk/.CLASSPATH
java -jar java_file.jar "%classpath%"
( the java jar file needs to take the location of the .CLASSPATH file as the argument ) .
In my main batch file, classpath variable takes the value depending on which project is checked out from the SVN .
Hope my question is clear.

That's because your set command has spaces around the =. Remove them and it works.
What you have there now essentially means that an environment variable classpath<SPACE> is set to the given value which starts with a space. Therefore, the environment variable %classpath% (without the space in the variable name) doesn't exist at all.

Related

Python Subprocess call to invoke java jar files with JAVA_OPTS

I am trying to make a subprocess call from my python script to a java class built inside a jar. I am running the python code on a docker container in AWS Batch. I set the CLASSPATH environment variable in the Dockerfile to include the directory containing the jar file.
ENV CLASSPATH /path/to/dir/containing/jar/file
When I pass the entire command with arguments as a string to subprocess, it works fine.
runnable_command = "java $JAVA_OPTS " \
"RunCommand " \
"-b arg_b"
sp = subprocess.Popen(runnable_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=True,
shell=True,
universal_newlines=True,
env=os.environ)
result, stderr_data = sp.communicate()
print(result)
But for that I had to make the variable "shell=True" which has a security risk. So I modified the variable 'shell=False" and I pass in the command and arguments as a list to the subprocess. This also works fine.
runnable_command = ["/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.x86_64/jre/bin/java", "$JAVA_OPTS", "<ClassName>"]
However, I am setting JAVA_OPTS environment variable in the Dockerfile to pass the log4j configuration file to JVM.
ENV JAVA_OPTS="-Dlog4j.configurationFile=/opt/amazon/lib/log4j2.xml"
This is important because I want to pipe the logs from this java script to my python script.
When I add JAVA_OPTS to the command, it fails with the following error:
runnable_command = ["/usr/lib/jvm/java-1.8.0-jdk-1.8.0.252.b09-2.x86_64/jre/bin/java", "$JAVA_OPTS", "<ClassName>"]
I am not able to pass JAVA_OPTS to the list of args in the subprocess command. It fails to find the log4j.xml file. I followed this question from stackoverflow but it fails with the same error, even after adding the JAVA_OPTS to the "env" argument.
'Error: Could not find or load main class $JAVA_OPTS\n'
Also, when I pass the arguments as a list, I am not able to run 'java' but I am forced to pass the absolute path of java executable.
Can someone help me with the following questions?
How can i pass the log4j configuration to this java command?
Why am I having to pass the absolute path to java command when running subprocess with a list and not a string?

Setting classpath from bat file doesnt work [duplicate]

This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 4 years ago.
So when i manually start my bat file that is on desktop, it works because executing a bat file via its icon uses directory of the icon(file) which is "C:\Users\Michael\Desktop".
set CLASSPATH = %~dp0 &:: %~dp0 stands for the directory of the bat file
java InitArray 5 0 4 &:: comment
pause
When i run the bat file above(InitArray.bat) from its desktop icon, it works.
java InitArray 5 0 4 &:: comment
pause
When i run the bat file above(test.bat) from its desktop icon, it works.
But when i run InitArray.bat from task scheduler, it uses the directory "C:\WINDOWS\system32". But that shouldnt be a problem because the first thing the bat file does is "set CLASSPATH = C:\Users\Michael\Desktop\".
Here is the result of task scheduler trying to run InitArray.bat.
C:\WINDOWS\system32>set CLASSPATH = C:\Users\Michael\Desktop\
C:\WINDOWS\system32>java InitArray 5 0 4
Error: Could not find or load main class InitArray
C:\WINDOWS\system32>pause
Press any key to continue . . .
Now, i know i can fix this issue by adding "C:\Users\Desktop\" to environment variable CLASSPATH. But i shouldnt need to do that since i am manually setting classpath to desktop in the first line of my bat file before trying to run the java class.
When setting a variable, everthing from the beginning of the variable name, until the last typed character is used as part of the variable name, before the = and value after the =. So:
set CLASSPATH = Somepath
Will end up with a variable name %CLASSPATH % (note the trailing space) and value Somepath (note the starting space.
Even if you add an accidental space after the value, it will become part of it, So this
set CLASSPATH=Somepath will end up with value with a trailing space Somepath
So always leave no space before or after the = and always enclose your code in double quotes to eliminate whitespace:
set "CLASSPATH=Somepath"
or in your actual case, it should look like:
set "CLASSPATH=%~dp0"
Set the CLASSPATH along with the java execution command or change directory using cd command.
cd /path_to_required_folder // set current directory
java -cp /classpath_location/ test.jar your.package.MainClass [args...]
Hope this helps.

Why do I have to use java -cp . (class name) and not java (class name)?

I was just curious why on some systems like mine, I have to utilize java -cp . and simply using the java command in the terminal window doesn't work ?
When you are launching a JVM using the java command, the JVM's classpath is determined as follows:
If you use the "-jar" option, then the classpath consists of the JAR file itself, and together with the optional "Classpath" attribute in the JAR file.
Otherwise, if you use the "-cp" option, the option's value gives the classpath
Otherwise, if the CLASSPATH environment variable is set, then that gives the classpath
Otherwise, the classpath consists of just the current directory; i.e. ".".
Now you say that you have to explicitly give "-cp ." in order for the java command to execute your commands correctly.
The most likely explanation is that you have the CLASSPATH environment variable set to something inappropriate. When you run a java MyClass, it will be looking on the classpath specified by CLASSPATH ... and failing. But when you add "-cp .", you are saying "ignore CLASSPATH and just look in the current directory".
The option -cp is used to add a path to a directory or files that the Java Environment will load for only that execution. Those files "will contains the references to the libraries" you use in your program.
Alternatively to use the -cp you can set the classpath permanently. The way of setting it depends on the operating system you use.
More information here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Giving environment variables as arguments

I have a jar file that gets arguments from commandline and I want to give parameter that contains environment variable. Something like below:
java -jar MyDev.jar -f %PROJECT_HOME%/src/test
But in above case program creates a directory named %PROJECT_HOME% however I want that PROJECT_HOME value in system is /home/jack path. And program should follow /home/jack/src/test not %PROJECT_HOME%/src/test path.
How can I do that ?
Are you running this in a Unix shell? If so, I suspect you just want:
java -jar MyDev.jar -f ${PROJECT_HOME}/src/test
Using % is the Windows way of specifying environment variables - which doesn't appear to fit with a home directory of /home/jack...
The component responsible for environment variables substitution is the shell/command line processor (cmd.exe on Windows).
I wrote the following main method:
public static void main(String[] args) {
System.out.println(args[0]);
}
When I pass "%PATH%" as an argument, running it from within Eclipse prints out %PATH%. Running it from the command line prints out the actual path environment variable.
Note that you can access environment variables from your Java code by using System.getenv().
For example, System.out.println(System.getenv("PATH")) prints out the actual path variable both from Eclipse and from the command line.
One very likely cause for this could be that the variable PROJECT_HOME is not defined or has a misspelled name. Hence, unless you have already done so, you should do echo %PROJECT_HOME% right before you start the java program in order to ensure that the variable is defined.

Add jar files to class path

I am making a build script as a batch file (don't ask me why or suggest alternatives. You won't be helping). I have a variable called CLASSPATH that I use with the java compiler. CLASSPATH contains paths to numerous directories and jar files. In addition to those, I'd like to add every jar file in the .[some-long-path]\lib\ directory
It looks something like this:
SET /p dummy=%CLASSPATH%>classpath.tmp~<nul
SET WAR_LIB_PATH=war\WEB-INF\lib
DIR %WAR_LIB_PATH% /B | findstr /L ".jar" > jars.tmp~
REM Have to put it into an external file
FOR /f %%j in (jars.tmp~) do (
SET /p dummy=;%WAR_LIB_PATH%\%%j>>classpath.tmp~<nul
)
SET /P CLASSPATH=<classpath.tmp~
ECHO %CLASSPATH%
This ALMOST works. There are just two problems:
Somehow a space appears between entries, which ruins the classpath.
It abruptly ends after 1024 characters.
Can someone help me with this?
If you use java6, it's enough to write dir/* to include all jars in the dir
http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
For example, if directory foo contains
a.jar and b.JAR, then the class path
element foo/* is expanded to
A.jar;b.JAR
If you are running javac, then try using the -classpath command-line argument instead of the environment variable, since those variables are size-limited on different operating systems.
And purely as a side note, if you are running a program from a JAR (ex java -jar app.jar), you can add metadata do the JAR file that accomplishes the same thing.

Categories

Resources