Java run jar with main class

Run a JAR File From the Command Line

A JAR file is an archive that contains the classes and resources of a Java application. We can either treat it as a library so that others use it as a dependency. Or we can run it as an application. In this quick tutorial, we’re going to look at how we can run a JAR file from the command line.

2. Sample Application

Take a look at our sample application. It contains the Greeting class:

public class Greeting < public static void main(String[] args) < if (args.length != 2) < throw new IllegalStateException("Please enter your name and city."); >final String name = args[0]; final String city = args[1]; System.out.printf("Hello %s from %s%n", name, city); > >

Greeting defines a static main method and expects two command-line arguments representing name and city.

3. Run an Executable JAR

Every JAR file contains a MANIFEST.MF file that resides in the META-INF directory. When the manifest file contains a Main-Class entry, we call it an executable JAR. The main class holds the main method and serves as the entry point to our application.

Now, we’ll first create our executable JAR file registering Greeting as the main class. For this purpose, we’ll configure the Maven Jar Plugin:

 greet  org.apache.maven.plugins maven-jar-plugin 3.1.2   com.javabyexamples.java.jar.Greeting      

Remember that jar is the default packaging type for a Maven project and the Maven Jar Plugin is responsible for creating the final JAR file. With this configuration, we’re just modifying the plugin so that it adds the Main-Class entry as com.javabyexamples.java.jar.Greeting to the manifest file. We’re also naming the JAR file by setting build.finalName as greet.

After running mvn clean package, we acquire our jar file, greet.jar. Now, we’ll run our application from the command line using the java command:

java -jar greet.jar john "Sunny Beach"

Here, the -jar flag specifies that the application is in JAR format. We then specify the JAR file and pass the program arguments for Greeting. Note that the arguments are separated with space. If the argument itself contains a space, we use double-quotes. When the application is launched, the runtime system passes the command-line arguments to the application’s main method as a String array.

Hello john from Sunny Beach

4. Run a Non-Executable JAR

When the manifest file doesn’t include the Main-Class entry, the runtime system can’t know the entry point to the application. We can create such a non-executable JAR by removing the previous Maven Jar Plugin configuration since Maven produces a non-executable JAR by default.

If we try to run a non-executable JAR with the java -jar command, we get an error:

no main manifest attribute, in greet.jar

To remedy this problem, we must specify the main class explicitly in the command line:

java -cp greet.jar com.javabyexamples.java.jar.Greeting john "High Valleys"

In this command, the -cp flag defines the application classpath as greet.jar. Then we specify the main class, com.javabyexamples.java.jar.Greeting. Also, we pass the program arguments separating them with space.

Hello john from High Valleys

5. Summary

In this tutorial, we’ve looked at how we can run a JAR file from the command line. We also learned the difference between an executable JAR and a non-executable JAR.

Lastly, the source code for all examples is available on Github.

Источник

Saminda Wijeratne’s Weblog

Specifying the Main class to run in a jar file from command line

Recently i wanted to run a jar file where its Main entry is not specified in the MANIFEST file. I knew this can be done since i’ve done it before, but couldn’t remember how to do it. So as usual i googled it but could not find it.

The thing is that this is a scenario that can arise when there are multiple entry points (mutiple classes having static main methods) in the jar file.

Funny enough the solution was pretty simple. Eventhough one might think that you need to specify the extry point class and the jar file which it is contained, the truth is we look at that fact in a wrong angle. What we have to do is to specify the class name and then the class path which the jvm should look for relevant classes (thus our jar should be included in this path).

say we have a class foo.class (which has a static main method) and it is in the jar file bar.jar. Then to execute the jar using the static main method present in foo class

here the class name should be a fully qualified class name. You can add more paths to the -cp parameter seprating them from a “:”

Share this:

Like this:

This entry was posted on November 4, 2008 at 8:18 am and is filed under Java. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

23 Responses to “Specifying the Main class to run in a jar file from command line”

  1. Roman Ovechkin Says:
    December 3, 2008 at 9:00 am | Reply Thanks for your good idea!
  2. Lk Says:
    March 23, 2009 at 4:36 pm | Reply great great solution. thanks.i searched this for my boss at work but wont mind if i have a relationship with u.i jst want to LEARN. u have my email.
  3. dadox Says:
    April 1, 2009 at 1:43 pm | Reply thanks dude, just what I needed!
  4. Qman Says:
    April 9, 2009 at 8:37 pm | Reply Thanks. Simple and to the point.
  5. Sean Says:
    December 10, 2009 at 4:05 pm | Reply Succinct and to the point.
  6. Ragunath Jawahar Says:
    March 13, 2010 at 3:10 pm | Reply Nice work dude. Thanks, JIT (Just-In-Time) 😀
  7. Jack Says:
    March 16, 2010 at 11:29 pm | Reply I’m having a tough time reading your site – the post isn’t being displayed the right way. Might possibly be my web browser (I’m still limited to using firefox 2.5), but I thought I’d give you a heads-up anyway just in case it’s something to do with your theme
  8. Beth Anderson Says:
    October 24, 2010 at 6:16 am | Reply This is a very great post about wordpress. I surely love seeing and learning something new about it. Just subscribed and looking forward to read some more articles of yours. Good luck and I hope we can connect in one of these days.
  9. Ocky Says:
    November 24, 2010 at 4:42 am | Reply Thanks for your info. I’ve tried and succeed.
  10. fanalion Says:
    June 28, 2011 at 8:01 am | Reply Thanks for sharing this 😀
  11. Mario Says:
    July 28, 2011 at 1:43 pm | Reply You made my day. Thanks.
  12. Nandish Says:
    August 11, 2011 at 6:15 am | Reply hi thank you very much for sharing this!! i want to learn something more, please send me mails if you find any new things
  13. Dragon warrior Says:
    November 21, 2011 at 9:35 am | Reply thanks it helped me…..
  14. Richard Says:
    December 26, 2011 at 10:21 pm | Reply Could you help me please?…is not working… (I’m in Windows XP): C:\>jar -tf test.jar
    META-INF/
    META-INF/MANIFEST.MF
    test/
    test/MyClass.class
    test/MyClass.java C:\>java -classpath test.jar MyClass
    Exception in thread “main” java.lang.NoClassDefFoundError: MyClass
    Caused by: java.lang.ClassNotFoundException: MyClass
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: MyClass. Program will exit. C:\>
    • samindaw Says:
      December 28, 2011 at 12:14 am | Reply It looks like the main class specified is wrong in the Manifest.MF file. Check to see if it is “test.MyClass” not just “MyClass” (the class name should be a fully qualified name) HTH Saminda
  15. Bob Says:
    January 10, 2012 at 11:15 am | Reply Thanks, that was brilliant – just what I needed.
  16. Richard Says:
    January 10, 2012 at 3:40 pm | Reply Thanks samindaw, – just what I needed.
  17. tony Says:
    February 20, 2012 at 8:46 am | Reply it doesn’t work at all
    still can not find main class 😦
    • samindaw Says:
      February 20, 2012 at 2:59 pm | Reply Hi Tony,
      Whats the error you are getting? Try something simpler like a hello world and see if it works.
      Saminda
  18. Chansoo Lee Says:
    May 16, 2012 at 9:41 pm | Reply Thank you SOOOO much.
  19. Rakesh Says:
    December 24, 2012 at 7:42 am | Reply thank you… this was really helpful
  20. Atox Gatal Says:
    December 27, 2012 at 11:58 am | Reply Hey, I still got an error even through this process.. C:\Program Files (x86)\JavaEmulator.com\KEmulator>java -cp KEmulator.jar emulato
    r.Emulator
    Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Program Files (x86
    )\JavaEmulator.com\KEmulator\swt-win32-3346.dll: Can’t load IA 32-bit .dll on a
    AMD 64-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(Unknown Source)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at emulator.k.a(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.(Unknown Source)
    at org.eclipse.swt.widgets.Display.(Unknown Source)
    at emulator.ui.swt.aK.(Unknown Source)
    at emulator.Emulator.main(Unknown Source) I tried to run KEmulator in manny ways, but still can’t spot the error.. Please e-mail me asap… 🙂
    • Pico Pron Says:
      January 30, 2014 at 10:10 pm | Reply I know this is two years late, but here’s the issue: “Can’t load IA 32-bit .dll on a AMD 64-bit platform”. Whatever KEmulator is, it referencing a 32-bit dll and you’re running on a 64 bit system. It’s not a java issue.

Источник

Running JAR-Packaged Software

Now that you have learned how to create JAR files, how do you actually run the code you packaged? Consider these scenarios:

  • Your JAR file contains an applet that is to be run inside a browser.
  • Your JAR file contains an application that is to be started from the command line.
  • Your JAR file contains code that you want to use as an extension.

This section will cover the first two situations. A separate trail in the tutorial on the extension mechanism covers the use of JAR files as extensions.

Applets Packaged in JAR Files

To start any applet from an HTML file for running inside a browser, you use the applet tag. For more information, see the Java Applets lesson. If the applet is bundled as a JAR file, the only thing you need to do differently is to use the archive parameter to specify the relative path to the JAR file.

As an example, use the TicTacToe demo applet. The applet tag in the HTML file that displays the applet can be marked up like this:

If the TicTacToe demo was packaged in a JAR file named TicTacToe.jar, you can modify the applet tag with the addition of an archive parameter:

The archive parameter specifies the relative path to the JAR file that contains TicTacToe.class. For this example it is assumed that the JAR file and the HTML file are in the same directory. If they are not, you must include the JAR file’s relative path in the archive parameter’s value. For example, if the JAR file was one directory below the HTML file in a directory called applets, the applet tag would look like this:

JAR Files as Applications

You can run JAR packaged applications with the Java launcher (java command). The basic command is:

The -jar flag tells the launcher that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all of the application-specific code.

Before you execute this command, make sure that the runtime environment has information about which class within the JAR file is the application’s entry point.

To indicate which class is the application’s entry point, you must add a Main-Class header to the JAR file’s manifest. The header takes the form:

The header’s value, classname, is the name of the class that is the application’s entry point.

For more information, see the Setting an Application’s Entry Point section.

When the Main-Class is set in the manifest file, you can run the application from the command line:

To run the application from the JAR file that is in another directory, you must specify the path of that directory: java -jar path/app.jar

Источник

Читайте также:  Pass arguments php command line
Оцените статью