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
Related
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 have a source folder (src) containing a jar file and many other folders of java codes. I have made a batch file which executes the following command perfectly fine while being in the "src" folder.
java -mx6g -cp .:trove.jar testing.Tester /somepath/myfile.txt
However, when I want to execute this batch file from a different path, even if I add the complete address, it still doesn't work. For instance:
java -mx6g -cp .:/Programs/src/trove.jar testing.Tester /somepath/myfile.txt
Even changing to this doesn't work:
java -mx6g -cp .:/Programs/src/trove.jar /Programs/src/testing/testing.Tester /somepath/myfile.txt
I get the error: Error: Could not find or load main class testing.Tester.
It may help you:
Syntax for "executable" JAR files:
java [ <option> ... ] -jar <jar-file-name> [<argument> ...]
e.g.
java -Xmx100m -jar /usr/local/acme-example/listuser.jar fred
Class and Classpaths are specified in the MANIFEST of the JAR file
You have to give fully specified path
java [option]/Programs/src/:/Programs/src/trove.jar testing.Tester /AbsolutePath/fileName.txt
the dot at the start of the classpath means current directory (src). you may need to fully specify that path as well.
java -mx6g -cp /Programs/src/:/Programs/src/trove.jar testing.Tester /somepath/myfile.txt
If you have a jar file called myJar.jar located in /myfolder and you want to use the class called myClass from it, how do you go about doing it from the command line?
I thought it would be to go into the directory and say java -cp myJar.jar.myClass but that isn't working. Any help would be appreciated.
Use java -cp myjar.jar com.mypackage.myClass.
If the class is not in a package then simply java -cp myjar.jar myClass.
If you are not within the directory where myJar.jar is located, then you can do:
On Unix or Linux platforms:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass
You want:
java -cp myJar.jar myClass
The Documentation gives the following example:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
There are two types of JAR files available in Java:
Runnable/Executable jar file which contains manifest file.
To run a Runnable jar you can use java -jar fileName.jar or java -jar -classpath abc.jar fileName.jar
Simple jar file that does not contain a manifest file so you simply run your main class by giving its path java -cp ./fileName.jar MainClass
Assuming you are in the directory where myJar.jar file is and that myClass has a public static void main() method on it:
You use the following command line:
java -cp ./myJar.jar myClass
Where:
myJar.jar is in the current path, note that . isn't in the current path on most systems. A fully qualified path is preferred here as well.
myClass is a fully qualified package path to the class, the example assumes that myClass is in the default package which is bad practice, if it is in a nested package it would be com.mycompany.mycode.myClass.
This is the right way to execute a .jar, and whatever one class in that .jar should have main() and the following are the parameters to it :
java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar
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.
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).