I have a stanalone application with a main class which used to run a windows BAT file ,the BAT file which invoke another java class(B),the class B refer so many JARs and configuration files which i configured through "build Path"
Now I want to refer the JARs and configuration files in BAT file.How I write the BAT file.
If you can make sure the .bat file and all needed jars are located in the same directory, this is quite easy:
rem sets the basedir to the directory where this batch file is locaed
set basedir=%~dp0
rem build the classpath for the Java command
set cp=%basedir%\jar_one.jar
set cp=%cp%;%basedir%\jar_two.jar
set cp=%cp%;%basedir%\jar_three.jar
(and so on...)
rem start your second class
java -cp %cp% your.package.ClassB
Is the program specific to one machine or does it need to be generic?
The issue would be, I think, would be how to get a jar file to return something outside of java. The obvious answer would be, use the batch file to run the java. Write the java so that it writes its 'return value' to a file. Write the batch file so that it watches for the file/dir that the java writes to, and looks for the changed/new data and then use it.
REM if all jar files contains in ./lib dir:
setlocal enabledelayedexpansion
for /r "./lib" %%a in (*.jar) do (set "CP=%%a;!CP!")
REM start your second class
java -cp %CP% your.package.ClassB
Related
I have a bat file I run through command prompt to deploy a java app locally for local testing on my machine:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-version.jar
exit
To run this bat file, I use the following command:
start batFileName.bat
However, the next time the version changes on this jar, the bat file will not work, because the version is out of sync. This results in myself having to change my bat file each time the version is updated.
Is there a way to pass in a the version when I run the start command through command prompt to use as the jar name? This way when I run my bat file, I can just pass in the name of the jar at that time to run the java application? If so how would I pass that version into the bat file and how would I use that parameter?
In your script, replace the version part of the jar file name with an argument replacement parameter:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-%1.jar
Do not start the program using java -jar . Change the start up script
include the folder where you jar file is present into class path with wild card, like:
java -cp path\to\jar*
call the main class in your jar file. I suppose the main class does not change so often as versions of the jar file?
The whole command line will look like this:
java -cp path\to\jar* com.something.foo.bar.Main
JVM will load your jar whatever its name is, and will find the main class and will start it if it has "main" method.
I created a simple batch file to run jar server file. It is two lines of code:
cd /D %~dp0
java -jar server-0.17.0.jar
The problem is the server file is updated every while and hence script either fails to run the correct version or fail to run.
My request is, is there anyway to replace server jar file with latest released by number or date dynamically?
I'm only aware that I can retrieve available jar files using:
dir *.jar
But have no idea how to concatenate the command with the katest version. Thanks
you can try a script like this:
#echo off
for /f "delims=" %%x in ('dir /od /b server*.jar') do set latestjar=%%x
java -jar %latestjar%
Second line in this script will set the latestjar to the last created file whose name compliant with server*.jar template.
In a former version of our application, we used the Eclipse launcher on Windows for startup. We hand the launcher an .ini file with java properties via the --launcher.ini property.
Now, we cannot use the launcher anymore and start the application from a .bat file using a java -jar <file> command. Therefore, the properties in the .ini file have to be added to this java command individually.
My idea is to have the .bat script read the properties from the .ini file and store them in a variable as list. The content of this variable is then used as part of the java command. How to do that in a robust way?
If you start it via a *.bat script I would first check if the file exists using:
if not exist myfile.ini (
# print some error
)
and if it exist I would iterate parse each line by:
FOR /F %i IN (myfile.ini) DO #echo %i
After that I would fill an array with the values and pass it to the java invocation.
I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):
CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g
But I am having the following problems when running in linux:
(1) I can't set a directory name in a classpath, the following line reports an error:
setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*
(2) when I run explictly with -jar option, I still get an error:
java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7): cannot find or open file: org.antlr.Tool
However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:
...
org/antlr/Tool.class
So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?
To specify multiple jars in a directory, directly in the java command, use this
java -cp "/sw/jar/*" org.antlr.Tool Calc.g
This will include all the jars
If you want to set the classpath in Unix/Linux systems, use this
export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar
in unix use this to set the classpath:
export CLASSPATH=myClassPath
about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?
if you want the path to be relative to your folder try:
java -jar ~/mypathToMyJar
I want to run a simple executable jar like this:
java -jar /path/to/my/jar/myjar.jar
However, i have to put that absolute path every time I want to run it. Is there a way to tell the OS to look for the jar in $PATH environment variable?
You can also copy your jar file to /usr/bin folder, then it will work with just $ myjar.jar
Make sure your jar file has the executable bit (chmod +x myjar.jar) and copy it to /usr/bin (sudo cp myjar.jar /usr/bin/myjar.jar)
I think you'd use the classpath; append your directory to it and java -jar myjar.jar should work.
You could use CLASSPATH environment variable instead
The -jar directive overrides the classpath, using whatever is defined in the jar file itself. The best way to do what you need is to use a tool such as ant, a script, or copy it to somewhere where the OS can find it (as Nejc Saje suggested).