Generated Runnable jar: Could not find the main class - java

I've always used the Export -> Runnable jar option of Eclipse and it has worked perfectly, but today it seems that all the Runnable jars I create produce the following error when I run them: (Edit: run them by double clicking the file.)
Java Virtual Machide Launcher
Could not find the main class: (class). Program will exit.
I've tried everything for the past two hours, but it still doesn't work.
I can still run jars that I have created in the past without a
problem.
The manifest file looks exactly the same as in those jars
that work and all the .class files seem to be in place...
I've tried exporting with Package required libraries into generated jar but the file still doesn't work. It doesn't show the error though - double clicking the file does nothing.
Edit: Running the jar file through the cmd works perfectly, but simply double clicking on it yields this error. (Bragboy)
Edit 2:
Contents of a manifest file from a jar that works: (Obviously they're the same, just wanted to avoid confusion.)
Manifest-Version: 1.0
Class-Path: .
Main-Class: defender.DefenderComponent (+ two empty lines afterwards)
And contents of a manifest file from a jar that doesn't work: (When opening the file directly)
Manifest-Version: 1.0
Class-Path: .
Main-Class: pixel.Entry (+ two empty lines afterwards)
Both main class files are confirmed to be at their respective locations.

While exporting the jar file, it is important to specify which is the main class. You can do this in the eclipse wizard. To get to this screen, you need to simply export as just a jar as opposed to Runnable jar.
One more option to find out the error, try to open a command prompt (in windows) or a terminal (in linux/mac) and give this command java -jar your_jar.jar . this will show a detailed error on what is happening.
Since your jar works properly when launched via command prompt, the definite culprit would be the default program opening for .jar extension in windows. To avoid this, you should probably have to right click on the jar and try open with a jar executable that is compatible with the java version that you ran the same from the command prompt.

In the end the only thing that fixed this was to switch from JRE 1.7 down to JRE 1.6.

I know this is a bit of a thread necro, but I was having the same problem. What finally worked for me was fixing my %JAVA_HOME% path in environment variables. Turns out it was still pointing to a 1.6 release, and that's why nothing since I installed 1.7 was running except through Eclipse where I had fixed all the references to point to the correct release.

Related

How to make my jar file executable by double click to open terminal

Okay so basically, every time I've seen this question asked it doesn't have the answer I'm looking for which is why I'm trying to ask myself.
Basically, I made a java project on BlueJ (required by school, sadly. But I can export it elsewhere if that's the problem (preferably IntelliJ or Visual Studio Code)), but essentially, I want to make it so I can simply double click my .jar export and it will open a terminal window and launch. When I try this however, I get this error: First Error
And when I click Ok then this appears:
Second Error
Now, if I want the jar to run on terminal, doing java -jar jarFileName.jar works perfect, but what I want is for that to happen when I double click the jar file; for it to launch and run from the terminal.
All the other answers I've seen for this problem blame the users computer and usually tell them to install some version of java or whatever, but this isn't the problem. I have other jar files that I can run perfectly fine, like Minecraft Forge installer or Minecraft Spigot installers (sorry only examples are Minecraft, not much else uses Java). And also this was for a uni project and no one else in my field know how to get their jar to be executable, nor does this file work for anyone else.
So yeah, I know it's a problem with how the jar was compiled or something and not what java is installed my PC.
I just want to make it double-clickable for the convenience of when I send it to friends, but if anyone knows another way I can get around this problem then that's fine. For example I know I could make a Bash file which simply does the java -jar jarFileName.jar for me or something, but I'd rather have it all as one jar file, so I don't know if this can be incorporated into the file but yeah.
Also I'd really like a solution which doesn't involve downloading external programs or whatever, since all I want to do is send this file to my friends and have them just double click it to launch.
TL;DR, what do I have to do to make my jar file actually executable by double clicking it. Thank you.
To make a jar file executable you have to set the entrypoint
You can do this by passing the e option to the jar command when you create the jar file, for example:
jar cvfe myapp.jar com.mycompany.myapp.MyApp com\mycompany\myapp
This will add the Main-Class entry in the jar's manifest pointing to the startup class, which should have a main method, for example:
package com.mycompany.myapp;
public class MyApp {
public static void main(String[] args) {
// start up the application
}
}
When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
The entrypoint is specified by the Main-Class, for example:
Manifest-Version: 1.0
Created-By: 1.8.13_37 (Oracle Corporation)
Main-Class: com.mycompany.myapp.MyApp
If you want to make an existing jar file executable you could unzip it, modify the manifest and zip it again (and rename it so it has the .jar extension - a jar is just a zip).
But I think the errors you show are not caused by the jar not being executable, but by some environment issue, causing the Java application to not find the necessary variables or paths to make the Java Native Integration (JNI) work. To solve this you would have to provide more detail on what you are trying to do with JNI.

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:
There is a class inside the jar that has a public static void main(String[] args) method inside.
That class is named (fully qualified) in the manifest of the jar, under key Main-Class
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).
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.
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

Java11/JavaFX11/Module Deployment Mess (Runnable Jar that works if clicked, but doesn't if ran from command line)

I'm new to Java11/all the overcomplicated module stuff.
The Problem
So I exported my Java11/JavaFX11 program from Eclipse as a Runnable JAR. If I click the JAR, it runs perfectly fine (Eclipse includes all of the module settings and JavaFX itself automatically in the runnable JAR). However, if I try to bundle the JAR with a JRE and run it via the command line with the following BAT file:
#ECHO OFF
%~dp0\jre\bin\java -jar javaprogram.jar
pause
I get:
Error: JavaFX runtime components are missing, and are required to run this application
Press any key to continue . . .
How can I get it to just run the JAR file like it does when I click it?
Ways I've tried to fix it
The weirdest part is, if I just use:
java -jar javaprogram.jar
Which just accesses the installed JRE, it works again. It's only when I'm directly pointing it to a JRE at a specific path that it appears to break.
Alternatively, I'd just bundle JavaFX beside the JRE, but there doesn't seem to be a way to call --module-path with a relative path (googling this nets me a bunch of entirely unrelated stuff). It seems to demand an exact path, which isn't going to work if people are downloading a zip archive and extracting it. This would be redundant though because Eclipse is already packaging JavaFX with the JAR. I don't know why it's getting confused just because I'm calling it from the command line.
The project's code
The project I'm trying to get this to work with happens to be open source, so you can check out the code for it here:
https://github.com/SkyAphid/JDialogue
The main class is JDialogueCore.
Closing
I don't want to use installers since I think that's too bloaty. I'd like to be able to deploy my software like I always have by just putting them in an archive you can extract and run.
It's difficult to simply Google the problems as well since I keep getting completely unrelated results due to the broadness of the topic. Any direction/documentation relating to this problem would be greatly appreciated.
Thank you for your time!
While Java 8, 9, and 10 allowed a JavaFX Application subclass to act as a main class for program startup, that is no longer the case as of Java 11. Placing your public static void main method in a different class and making that class the main class solves the problem. (Source: https://github.com/javafxports/openjdk-jfx/issues/236)
Your command line invocation needs to specify both the location of the JavaFX jar files, and the location of JavaFX native libraries. Normally these are the same location in the JavaFX SDK, but they must be specified in different ways: the jar files go in the classpath or module path, while the native libraries’ location must be specified in a system property:
cd /d %~dp0
jre\bin\java -cp javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib com.example.MyNonApplicationClass
If you define a module-info.java in your program, your .jar is a modular .jar, and you can benefit from the additional security of modules:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp/com.example.MyNonApplicationClass
If your modular .jar file has a main class defined, you can omit the class name:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp
Notice that relative paths work just fine with --module-path. Relative paths use the current directory as a base. The current directory is not changed merely by putting %~dp0 in front of the invocation of Java. The current directory is a property of the command line or script actively running, and can only be changed with commands like cd or pushd.

Java application will run from CMD and Eclipse but not double click

I've done a search and I can see that a lot of people have had the same problem as me, but none of the solutions have worked for me.
Basically I have a Java Project in Eclipse that is from my old Windows Installation. I've cleaned and rebuilt it because at first it wouldn't compile, but now I have it exported as a Runnable Jar. However, the only way I can get the application to appear is to do java -jar foo.jar in command prompt, or run it in Eclipse. If I double click the JAR in Windows Explorer nothing happens even though I know that Java is associated correctly because other Runnable Jars work.
The project only has the x86 JRE listed in it's Build Path Libraries and all the files listed appear to exist. I'm running Windows 7 HP.
Update: I'm sorry, but I just discovered that no other Runnable Jars are working either. If they are wrapped with launch4j they work though...
Edit: The Runnable Jars that I export from Eclipse do work fine on other systems and load on double click
Some registry values or file associations are probably messed up. Wiping off all of your existing JRE's and JDK's and re-installing them should fix your issue.
Alternatively you may be able to fix it by manually editing the registry value here:
HKLM > SOFTWARE > Classes > jarfile > shell > open > command
My value is
Type: REG_SZ
Data: "C:\Program Files\Java\jre8\bin\javaw.exe" -jar "%1" %*
You'd of course want that path to point to your javaw.exe, and make sure you have the additional arguments.
I was also facing the same problem while i was working with Spring tool suite.
You may use the following steps:-
Right click on project -> export -> Runnable jar file -> (Here,In library handling,there are three options,you have to choose middle one i.e package required library into generated jar.It will package external dependency also).
-In my case, my runnable jar was only executing on my environment i.e on which i have created that JAR. Initially i have selected the first option to create JAR i.e extract required libraries into required JAR.but that was not proper.
It may help you.Let me correct if i am getting wrong.
Since you are able to run the JAR running the command line, I believe your issue is related to which version of Java is set to run the file when it is double-clicked.
To find out which version is successfully running the file from the command line and set it to open JAR files by default:
Open a new command prompt window.
Run echo %path%. Among the path values, you should be able to find one pointing to the bin folder of one of the installed versions of Java. Copy this path somewhere.
Navigate to the JAR file you would like to run. Right click the JAR -> Open with -> Choose default program... -> Browse...
Browse to the path you copied in step 2. (the easiest way is to paste it into the address bar)
Double click javaw.exe.
Click OK.
You should now be able to run the JAR file. Please let me know if your problem persists.
I have made a jar with and keep it on desktop.Then,I double clicked on the jar and it working fine for me.
How I and what I have monintored: In my main class, perform some operation and at the end I add on Thread.sleep(25000); to hold the program for few moments. After every double click on the exported jar I found one new javaw.exe process added in the system process tree. I have noticed it on Task manager. and after 25000ms respective javaw.exe process ended. As my application does not cointain any GUI that's why I have not seen any GUI changes for the respective process. I'm Confident that if my application have some GUI, I will surely get the respective GUI window on every run.
Common mistakes : when we export a project from Eclipse as Runnable JAR file, it is exported with selected Eclipse launch configuration and Eclipse specific launch wrappers. Now if the configuration does not match when you are trying to run it via double-click you will not be able to see the error, If you run it from CMD then surely you will get the error log.
To create standard executable JAR file : To create a standard executable JAR file, you can export as JAR file and specify the main class in last screen of the wizard.
That's working for me fine. I used the same jar from different system and keeping it different location.

Executing .jar files with a double click

I've been using Eclipse as my Java IDE and today I ran into an interesting problem. I used the built in file>export>runnable jar file to create a jar file for one of my programs. It runs perfectly if I start it from the command line/with a batch file, but doesn't run from a double click. I made absolutely certain that .jar files are associated with javaw and it still didn't work. It isn't a huge problem I absolutely NEED fixed, but if anyone has any ideas I'd love to hear them.
Update: I tried using Aram Kocharyan's solution (see answer below). No luck. It seems to be just this one .jar file too. All other jars launch with a double click. Rebuilding the jar doesn't help. :P
I wrote a short guide on this a while back you might find helpful:
http://ak.net84.net/projects/how-to-make-a-multi-platform-executable-java-jar-file/
This will work on double click in windows without setting any additional settings, and I've tested it with the Jar Launcher on Mac and it doesn't complain.
open your jar with..javaw
which is in bin folder of jre... in my computer it is #
C:\Program Files\Java\jre\bin\javaw.exe
Put this inside a .bat:
start javaw -classpath "%~dp0YOU-JAR-NAME.jar" -Djava.library.path="native" foo.package.bar.YourClassWithMainMethod
the option -Djava.library.path="native" its not required for run, i include this because one reason for using a .bat instead do a double click on a .jar is to use JVM-parameters, in my case i need this parameter for my project run.
the %~dp0 part get current directory

Categories