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
Related
I download source code from here http://algs4.cs.princeton.edu/code/
My folder contains three files: algs4.jar, java file and input txt file.
When I type this command line in cmd
javac -cp .;algs4.jar JavaFile.java
It's ok.
But then, I type
java -cp .;algs4.jar JavaFile.java InputFile.txt
That don't work. It has an error: "Could not find or load main class ..".
Can somebody help me ? :)
Assuming JavaFile.java has a main method use
java -cp .;algs4.jar JavaFile InputFile.txt
int the manifest of algs4.jar do not assign the main class, so you use -cp option to point it out, but the main class should have a full name, including the full package name.such as:
java -cp algs4.jar edu.princeton.cs.algs4.AcyclicLP
I have a java class I'm trying to run that references various other jar files. It looks something like this:
package com.myapp.test;
import java.io.File;
import com.myapp.ref;
import com.myapp.stuff;
import com.strangersapp.stuff;
import com.strangersapp.morestuff;
public class myTest {
public static void main...
...
}
The com.myapp.* classes are in another jar file called myapp.jar. The strangersapp classes are in a strangersapp.jar. There are several other referenced classes in various jars. My whole directory structure looks like this:
myapp.jar
strangersapp.jar
someother.jar
yetanother.jar
etc.jar
com\myapp\test\myTest.java
My problem is trying to compile and run this. I try the obvious but this command does NOT work. It doesnt pickup all the jar files:
javac -cp . com\myapp\test\myTest.java
java -cp . com.myapp.test.myTest
This command also does not work:
javac -cp ".;*.jar" com\myapp\test\myTest.java
However, these commands do work:
javac -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com\myapp\test\mTest.java
java -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com.myapp.test.mTest
Would anyone know why my first java run statement is not working?? I dont want to type all the jar names out in the classpath reference...
You should try this:
javac -cp ".;*.jar" com\myapp\test\myTest.java
java -cp ".;*.jar" com.myapp.test.myTest
If you use -cp you have to name all the jars you want to include. To avoid that just add them to your manifest and java will pick them up.
For javac i think you are supposed to use -classpath rather than -cp
I want to run a Java project from the command line which I start using a batch file, but I get the wrong name error.
The directory setup:
srcMVC
bin (folder with .class files)
src (folder with .java files)
Batch file
Batch file:
set path=C:\Program Files\Java\jdk1.7.0_09\bin
javac src\model\*.java -d bin -cp src
javac src\controller\*.java -d bin -cp src
javac src\view\*.java -d bin -cp src
javac src\main\*.java -d bin -cp src
PAUSE
java bin\main.Main
PAUSE
Compiling works, but I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: bin\main/Main (wrong name: main/Main)
Any suggestions?
package main;
// omitted imports
public class Main {
// omitted variables
public static void main(String[] args) {
// omitted implementation
}
}
The following statement resolved my error:
java -cp bin; main.Main
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.
For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
By default Java CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.
So, You need to add other paths to CLASSPATH at run time. Read more Setting the classpath
java -cp bin main.Main
where Main.class contains public static void main(String []arg)
you are wrongly exicuting java bin\main.main
main() is your main method but you should supply java interpreter the Class Name which implements main()
So if your class name is Test and file name is Test.java which has main() method
java Test
if your Test.java/Test class in is package my.test e.g - package com.my.test;
than, java com.my.test.Test
hope you got it !!
java bin/main.Main is wrong, you must specify -cp here:
java main.Main -cp bin
Here the first argument is the class name which should be found in the classpaths, rather than the class file location. And -cp just adds the logical path to classpaths. You should make the root of your project searchable in the classpath.
and for those javac commands, you have already specified the correct path, so you don't need -cp src. The difference here is the javac command uses logical path for .java files, while using java command you could only specify the path in -cp attribute.
You could also execute java main.Main without -cp if you enter the directory bin:
cd bin
java main.Main
Since the current path will be automatically be searched by java as a classpath.
Assuming you have a class called Main you have to run it with this command:
java bin\Main
It will call your main method.
Java run time (in your case the java.exe command), takes the class file name that containst the main() method as input. I guess you should be invoking it as "java bin\main" assuming there is a main.class which has a public static void main (String[]) method defined.
Note: General practice is to capitalize the first literal of any class name.
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
I have a JAR with 4 classes, each one has Main method. I want to be able to run each one of those as per the need. I am trying to run it from command-line on Linux box.
E.g. The name of my JAR is MyJar.jar
It has directory structure for the main classes as follows:
com/mycomp/myproj/dir1/MainClass1.class
com/mycomp/myproj/dir2/MainClass2.class
com/mycomp/myproj/dir3/MainClass3.class
com/mycomp/myproj/dir4/MainClass4.class
I know that I can specify one class as main in my Manifest file. But is there any way by which I can specify some argument on command line to run whichever class I wish to run?
I tried this:
jar cfe MyJar.jar com.mycomp.myproj.dir2.MainClass2 com/mycomp/myproj/dir2/MainClass2.class /home/myhome/datasource.properties /home/myhome/input.txt
And I got this error:
com/mycomp/myproj/dir2/MainClass2.class : no such file or directory
(In the above command, '/home/myhome/datasource.properties' and '/home/myhome/input.txt' are the command line arguments).
You can create your jar without Main-Class in its Manifest file. Then :
java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2 /home/myhome/datasource.properties /home/myhome/input.txt
You can execute any class which has a public static void main method from a JAR file, even if the jar file has a Main-Class defined.
Execute Main-Class:
java -jar MyJar.jar // will execute the Main-Class
Execute another class with a public static void main method:
java -cp MyJar.jar com.mycomp.myproj.AnotherClassWithMainMethod
Note: the first uses -jar, the second uses -cp.
This answer is for Spring-boot users:
If your JAR was from a Spring-boot project and created using the command mvn package spring-boot:repackage, the above "-cp" method won't work. You will get:
Error: Could not find or load main class your.alternative.class.path
even if you can see the class in the JAR by jar tvf yours.jar.
In this case, run your alternative class by the following command:
java -cp yours.jar -Dloader.main=your.alternative.class.path org.springframework.boot.loader.PropertiesLauncher
As I understood, the Spring-boot's org.springframework.boot.loader.PropertiesLauncher class serves as a dispatching entrance class, and the -Dloader.main parameter tells it what to run.
Reference: https://github.com/spring-projects/spring-boot/issues/20404
Apart from calling java -jar myjar.jar com.mycompany.Myclass, you can also make the main class in your Manifest a Dispatcher class.
Example:
public class Dispatcher{
private static final Map<String, Class<?>> ENTRY_POINTS =
new HashMap<String, Class<?>>();
static{
ENTRY_POINTS.put("foo", Foo.class);
ENTRY_POINTS.put("bar", Bar.class);
ENTRY_POINTS.put("baz", Baz.class);
}
public static void main(final String[] args) throws Exception{
if(args.length < 1){
// throw exception, not enough args
}
final Class<?> entryPoint = ENTRY_POINTS.get(args[0]);
if(entryPoint==null){
// throw exception, entry point doesn't exist
}
final String[] argsCopy =
args.length > 1
? Arrays.copyOfRange(args, 1, args.length)
: new String[0];
entryPoint.getMethod("main", String[].class).invoke(null,
(Object) argsCopy);
}
}
Another similar option that I think Nick briefly alluded to in the comments is to create multiple wrapper jars. I haven't tried it, but I think they could be completely empty other than the manifest file, which should specify the main class to load as well as the inclusion of the MyJar.jar to the classpath.
MyJar1.jar\META-INF\MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.mycomp.myproj.dir1.MainClass1
Class-Path: MyJar.jar
MyJar2.jar\META-INF\MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.mycomp.myproj.dir2.MainClass2
Class-Path: MyJar.jar
etc.
Then just run it with java -jar MyJar2.jar
First of all jar creates a jar, and does not run it. Try java -jar instead.
Second, why do you pass the class twice, as FQCN (com.mycomp.myproj.dir2.MainClass2) and as file (com/mycomp/myproj/dir2/MainClass2.class)?
Edit:
It seems as if java -jar requires a main class to be specified. You could try java -cp your.jar com.mycomp.myproj.dir2.MainClass2 ... instead. -cp sets the jar on the classpath and enables java to look up the main class there.