Run class in Jar file
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
How to run a class from Jar which is not the Main-Class in its Manifest file
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
Run a JAR file from the command line and specify classpath
When you specify -jar
then the -cp
parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
- include all jar files from the
lib
directory into the manifest (you can use relative paths there) - Specify everything (including your jar) on the commandline using
-cp
:java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
How to run java class from .jar archive?
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
.
Why should I run a java class after generating jar file with maven package?
To know the answer of Why, you need to understand following things first:
How to compile a Java program and how to run the compiled java program (especially program declared inside a package)
If you have java classTest
inside packagehello
, you can compile it usingjavac hello\Test.java
and run it usingjava hello.Test
(i.e. we use fully qualified name to run a program and can not run it likejava Test
, it will give you error "could not find or load main class Test")
So, just going to src/main/java/com/mycompany/app and runningjava App.java
won't work but from src/main/java folder you can run it likecom.mycompany.app.App
What is jar packaging.
jar
i.e. Java archive is nothing but a zip file aggregating all the java classes so that it can be distributed as a single unit.Classpath
Classpath is the place where java will look for the compiled classesHow to run a program which depends on Java classes in other jar files
Suppose if my Test class depends on class XYZ which is inside the
abc.jar file, then we need to tell java that search this abc.jar for
dependencies (include this jar in classpath). This can be done using commandjava -cp abc.jar hello.Test
here-cp
option is nothing but a classpath and is used to tell java about directories or archives in which classes could be found. This command can be used when Test class is inside jar file like in your caseMaven
If you have understood the above things then you would know that Maven has nothing to do with running your program. It is just a build tool which helps build the jar file from you code and helps in executing/organizing different tasks apart from build like clean, running tests, etc.
How to run classes inside Jar file?
You can do that using Junit
library.
JUnitCore junit = new JUnitCore();
Result res = junit.run(yourTestClass);
Don't invoke the main() method to avoid any possible interruptions, just let the JUnitcore
find the appropriate test methods.
And also don't forget to include Junit.jar
in classpath (or) include it as part of your jar file (as a fat jar).
Related Topics
How to "Pretty Print" a Duration in Java
How Do Format a Phone Number as a String in Java
Execute Jsp Directly from Java
Check If File Exists on Remote Server Using Its Url
How to Get a Client's MAC Address from Httpservlet
Best Practice for Passing Many Arguments to Method
How to Convert an Integer to Localized Month Name in Java
Numeric Textfield for Integers in Javafx 8 with Textformatter And/Or Unaryoperator
How to Get Http Response Code for a Url in Java
Right Way to Write JSON Deserializer in Spring or Extend It
Java Bigdecimal: Round to the Nearest Whole Value
How to Get Subnet Mask of Local System Using Java
What Does Super.Paintcomponent(G) Do
How to Add Days to a Date in Java
Dynamically Find the Class That Represents a Primitive Java Type
How to Set Up Default Schema Name in JPA Configuration