Run Class in Jar File

Run class in Jar file

Use java -cp myjar.jar com.mypackage.myClass.

  1. If the class is not in a package then simply java -cp myjar.jar myClass.

  2. If you are not within the directory where myJar.jar is located, then you can do:

    1. On Unix or Linux platforms:

      java -cp /location_of_jar/myjar.jar com.mypackage.myClass

    2. 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:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. 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:

  1. How to compile a Java program and how to run the compiled java program (especially program declared inside a package)

    If you have java class Test inside package hello, you can compile it using javac hello\Test.java and run it using java hello.Test (i.e. we use fully qualified name to run a program and can not run it like java 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 running java App.java won't work but from src/main/java folder you can run it like com.mycompany.app.App

  2. 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.

  3. Classpath

    Classpath is the place where java will look for the compiled classes

  4. How 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 command java -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 case

  5. Maven

    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



Leave a reply



Submit