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.
Related
I have the following script in a bat file.
path=C:\Program Files\Java\jdk1.8.0_181\bin
prompt $$
cls
javac GUI.java
echo Main-Class: GUI>manifest.mf
jar cmf manifest.mf GUI.jar GUI.class
java -jar GUI.jar
pause
The Java file is compiled and the manifest file is created. However, when it comes to creating the Jar file, the command prompt gets stuck (hangs). It behaves as if it were on a loop.
! [Command Prompt Output]: https://i.stack.imgur.com/uO7gc.jpg
Any help would be appreciated.
According to this (under jar command options > m) you should switch the manifest and jar file name, so jar cmf GUI.jar manifest.mf GUI.class
The script was named 'jar' which caused problems. I renamed the file and it worked.
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
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
My jar file P2.jar (which has a main method and manifest file) is located in C:\Jar Folder. I tried to run it from cmd like this:
C:SomeRandomFolder> java -jar C:\Jar Folder\P2.jar
I get the error:
unable to access jarfile C:\Jar
It works when I run it from the folder that the jar is in like this:
C:\Jar Folder> java -jar P2.jar
Why can't I use the 1st command? Why do I have to go to the directory of my jar file?
That's because of the space you have in the path name. On Windows, use quotes:
java -jar "C:\Jar Folder\P2.jar"
If you are in a terminal and the folder you are in is deleted out from underneath you and replaced with the same content, the jar file will be be visible with ls, but will not be available since your current directory was deleted and replaced with one that looks just like it.
I got that error too:
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Unable to access jarfile CalculateStats.jar
To fix it. cd up a directory and cd back in, like this:
el#apollo:/myproject/build/jar$ cd ..
el#apollo:/myproject/build$ cd jar/
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Program completed successfully
Need to run particular java class from jar package through unix console. Is it possible? Thanks
It's not possible to call a specific method of a class from a terminal.
You can only call the main method with java -cp JarFile.jar package.ClassName
Or, if your jar contains a manifest file, then you can do: java -jar pJarFile.jar.
Use
java -cp myJar.jar myClass
The -cp option is used when setting up the classpath manually ie give full path of location of jar file and then run command
Eg.
C:> java -cp C:\java\MyClasses\myJar.jar myClass
For running a jar file you can have either of following approach. Both of them require that you know the fully qualified name of the class where main(String[] arg) method is written.
Say your class containing main method is com.myclass.MainClass
You can run the jar directly. Keep the jar file at the location from where you are running this command
java -cp yourjarfile.jar com.myclass.MainClass
Create a manifest manifest.mf file with following content
Main-class: com.myclass.MainClass
now create the jar file as follows
jar -cmf manifest.mf yourjarfile.jar <your class file location>
Run this jar with the following command
java -jar yourjarfile.jar
Yes, use the -jar parameter:
java -jar MyProgram.jar
you can extract the specific class file by doing:
jar xf MyProgram.jar theclass.class
it will end up in your current directory
then just run the classfile
java theclass