简体   繁体   中英

JAR File is not Executing

I am Cleaning and Building a project, which is creating its .jar file in its "dist" folder. However the issue is that, I'm not able to run it. I double click on it and nothing happens.

I have set up "bin" folder of JDK in "Path" option in environment variables.

Is there is anything that I am missing? I am new to all this and help is really appreciated.

Suggested Learn how to make executable file yourself , rather than depending on auto generated jar file

Here is a tutorial.

In case of default jar file , most likely you have to execute

java -jar MY_AWESOME_JAR_FILE_NAME.jar

There are 3 common reasons for this:

On windows, and using sysin/sysout

Windows, because apparently microsoft is not capable of fixing ancient silliness, forces upon apps that they either have a terminal in which case an ugly black box always pops up, or they don't, and can't later make one. That means that on windows only, there are 2 java executables: java.exe and javaw.exe , with as only difference that javaw doesn't get a box. But, it doesn't get a box - sysin and sysout do pretty much completely nothing.

By default, double clicking a jar starts it with javaw, which means if your app's only interaction is reading from System.in and writing to System.out or System.err , you won't see anything.

There is no fix to this, other than to make a GUI app, or make a batch file (a windows only concept) that explicitly runs java.exe .

This doesn't apply to linux or macs.

jar file broken

A runnable jar file is one that has three properties:

  1. There is a class inside the jar that has a public static void main(String[] args) method inside.
  2. That class is named (fully qualified) in the manifest of the jar, under key Main-Class
  3. Any deps needed are either baked into the jar, or in another jar, and those other jars are named, space-separated and relative to the dir that the jar you're double clicking lives in, in the manifest, under key Class-Path . Note that the global environment variable CLASSPATH does nothing when double clicking jars.

You can check all this; jars are just zip files. Also, the jar tool in your JDK can unpack them, and the manifest is just a file named META-INF/MANIFEST.MF inside. You can open it up, have a look if it's properly configured.

java install broken

Check any random runnable jar first. Maybe you installed a headless java. Note that these days (since JDK9 and up), an 'end user' wouldn't even install a java and the notion of double clicking a jar to run it is basically obsolete. To make 'desktop' java applications, you'd ship an entire JRE (treeshaken if you want) and you're responsible for an installer. There are some limited tools in the JDK (since 9) that help you out (such as jlink ).

  1. Use the CLI to see whether your Java is set up properly, the jar is corrupted, or the jar is incompatible with installed Java version.
  2. Ensure the jar file is properly associated with Java binary. The instruction can be found here

Just edit manifest file adding main method in there. If you have any additional reference libraries simply put them into class path there. Manifest specificatin is located here: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM