JVM Launcher cannot find main class - java

I am using NetBeans to build my executable JAR and haven't messed around with any Ant or project settings (using defaults).
When I go to Run >> Clean and Build Project, I see NetBeans building my JAR and putting it into my project's dist/ directory without a hitch.
When I double-click that JAR to run it, I get the following error message:
Could not find the main class: com.me.myorg.MainApp. Program will exit.
If I open up the JAR in WinRAR and extract the META-INF/MANIFEST.MF file, I see the following attribute defined:
Main-Class: com.me.myorg.MainApp
Obviously, com.me.myorg.MainApp is the "head" of my GUI (Swing) app.
Any ideas as to what is going on? How to troubleshoot? Thanks in advance.

Try in command prompt(Windows):
ftype jarfile="C:\PATH_TO\javaw.exe" -jar "%1" %
EDIT
Run the command prompt like this: Go to Start and in the Search box type CMD. Right click the Command Prompt icon and choose Run as Administrator.
http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/
Then run the command above.

Is there a new line after the main class declaration?
Main-Class: com.me.myorg.MainApp
# empty line here
The manifest file format requires each line to end with a line separator. If the line with main class is the last line in the file it is ignored.

Related

Unable to run JAR file outside Intellij

I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version

Unable to run jar file from command line

I have an excel spreadsheet which launches several jar files from the command line. For some reason one of my jars has failed to run from the command line, yet when I double click on the executable, it runs fine.
I need the sheet to be able to run it from the command line. What could the issue be?
There are certain steps you need to perform to run jar through command line without specifying main class.
Add following line to your manifest file , while creating jar.
Main-Class : mypackage.myclass.yourclassnamehere
Here is the link [ http://www.mkyong.com/java/how-to-make-an-executable-jar-file/ ] for sample tutorial.
If you want to run from excel file, add java.exe as default program for jar extension executable. (In windows you can do it from control panel or right click on executable and select java.exe from other programs and ticking option as default program for .jar extension)

Jar file won't run via command prompt or double-click

I recently finished a project which works as it is supposed to in my Eclipse IDE as both multiple files and as a single file.
Eclipse exports the jar file and only makes noise about the warnings.
When I go to run the jar file with a double-click, the cursor seems to flash to a hourglass for less than a second and then nothing. When I try to run the jar file from the command line with java -jar myJarFile.jar the command prompt window seems to wait a second and then brings the file path line and cursor with no errors and no other messages.
I have double checked both my Path variable and that I have the latest version of Java installed.
Any suggestions?
Check this thread: Failing to execute the jar file using java -jar command
Basically, what you need to do is when you run the jar, you need to specify the class to be executed in the command line, like so:
java -cp test.jar com.app.ClassName
Did you just make a jar file, or a runnable jar file? If you only did the former then it will not execute on a double click. So it's Right Click the project in Eclipse -> Export -> Runnable JAR File -> Choose the appropriate Launch Configuration -> Choose where you want it to go.
Confirm that your application code is in the jar file:
jar tf myJarFile.jar
Confirm that your application's main class is listed in the Main-Class attribute in the manifest, and that any other jar files needed by your application are listed in the Class-Path attribute:
jar xf myJarFile.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF
Try running with verbose logging of classes being loaded; this should show if some necessary class can't be found:
java -verbose:class -jar myJarFile.jar

NetBeans-made jar file won't work

So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?
The things that seem to be needed in NetBeans are:
The project has to be the Main Project (by right clicking on it in the Projects list).
You have to set the Main Class in the project properties. (Right click, Properties, Run, Main Class.)
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist subdirectory.
If that fails to fix the problem, here's a longer story...
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
java -jar filename.jar
(When you say it works for you from the command line, is this what you're typing?)
At that point, the Java executable looks for a file inside the jar named META-INF/MANIFEST.MF. And then in the contents of that file, it looks for the value of a property, Main-Class. And finally it looks for the class of that name in your jar and runs its static main(String[]) method.
So if your jar is failing to run, you can do the following to debug what's going on:
Clean and rebuild your project in NetBeans.
Double check that your class(es) are actually in the jar:
Start a command prompt
cd into the dist subdirectory of your project.
Use a command like jar tf filename.jar to list what's in there.
Double check that the MANIFEST.MF file is correct:
Again in a command prompt
cd into the dist directory.
Use a command like jar xf filename.jar META-INF/MANIFEST.MF to extract the manifest.
Look at the contents of that file (e.g. type META-INF\MANIFEST.MF) and make sure Main-Class is set to the appropriate class.
If all of the above check out, then double clicking the file should work.
Have you set the containing project as the "Main Project"?

Cannot run my .jar file

I tried double clicking it, I didn't get anything, I tried opening it in CMD, I got this:
no main manifest attribute, in MyRestaurant.jar
What should I do ? :(
When you create a JAR you need to specify which class has the main method you want to run, this is done by adding a manifest.txt as a file to the jar you create. It seems thats the thing its missing.
Create a manifest.txt file, just add one line Main-Class: ClassName followed by enter and add that file when you create your jar.
ex. -jar -cvmf manifest.txt app.jar ClassName.class
If you are using Netbeans IDE, the first time you complied, choose Run then Netbeans will ask you the main class. From next time, you can just Clean and Build.

Categories