After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar
Related
I store jar files in C:\Users\myuser\javatools\avro-tools
And added them to my PATH:
echo %PATH%
...;
C:\Users\myuser\javatools\avro-tools;
I can run them by specifying the full path:
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar
But cannot run them without the full path:
java -jar avro-tools-1.8.1.jar
Error: Unable to access jarfile avro-tools-1.8.1.jar
I need to run jar files without changing to the directory, nor specifying these full paths.
UPDATE: Added %*
I'd recommend creating a batch file and run that instead.
avro-tools-1.8.1.bat
#echo off
java -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Place .bat file somewhere in PATH, and run by simply typing:
avro-tools-1.8.1.bat -abc def
The %* in the .bat file gets replaced with any argument passed to the .bat file, so the -abc def arguments are passed to the avro-tools program in the args array to the main method.
If you have multiple versions of Java installed, you can then choose which one to use when running that .jar file, by also qualifying the java command.
avro-tools-1.8.1.bat
#echo off
"C:\Program Files\Java\jdk1.8.0_181\java.exe" -jar C:\Users\myuser\javatools\avro-tools\avro-tools-1.8.1.jar %*
Now that code will run with Java 8, even if Java 8 is not the default Java on your machine.
[(It's sad this question has already been asked.)]
I've the batchfile D:\Hydroper\Projects\Java\ASC\bin\asc.cmd that executes a JAR file .\target\asc-1.jar in the same directory. Absolute path: D:\Hydroper\Projects\Java\ASC\bin\target\asc-1.jar.
However, when I execute my batchfile, I get the Unable to access jarfile error:
asc -strict Main.as
but my JAR is there. Even adding a .jar or executing with admin privileges the error persists. It was automatically built with Maven.
Here my batch:
java -jar "D:\Hydroper\Projects\Java\ASC\bin\target\asc-1" %*
It works when I do java -jar ... args manually in the command-line, but I don't like that. I prefer aliasing that command.
ClassPath
I've instead also tried something like:
java macromedia.asc.embedding.Main "%*"
It works fine, but ASC doesn't. asc -help works fine, but asc -help Main.as says my AS file cannot be found, but I'm in the right directory.
Simply do the below:-
cd D:\Hydroper\Projects\Java\ASC\bin
copy the asc.cmd file to bin folder and modify the batch file like below
java -jar target\asc-1.jar
First execute the above command from command prompt first before executing the same from batch file like below:-
cd D:\Hydroper\Projects\Java\ASC\bin
java -jar target\asc-1.jar
When we use a batch file or shell script to run a java application do it like below:-
in your batch file
CP="C:\lib\example1.jar;C:\lib\example2.jar;D:\Hydroper\Projects\Java\ASC\bin\target\asc-1.jar"
java -cp %CP% com.your.java.class.MyExample
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 want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
I am trying to create a jar file off of a class and xml file I have in a directory called SOACustomFunction.
OS: Windows 7 Enterprise - 64 Bit
Java: jdk1.6.0_21
I tried using the command below and I keep getting the error:
Error: "Unable to access jarfile SOACustomFunction.jar"
C:\SOACustomFunction>"C:\Program Files\Java\jdk1.6.0_21\bin\java.exe" -jar -cvf
SOACustomFunction.jar *.*
Any ideas what I am doing wrong?
Thanks,
S
The command you show is what you would use to EXECUTE the jar file. Use jar.exe instead of java.exe -jar
Typically you would have
jar cvf yourJarFile.jar yourClass.class yourSecondClass.class
java -jar is for running a class from a jar file.
Assuming your classes and xml reside inside your SOACustomFunction directory, the command would be
cd SOACustomFunction
jar cvf myjar.jar yourclass.class yourxml.xml
But do maintain a package structure for your classes and other resources instead of having them all in the jar's root directory.