Cannot find main class, java error - java

I have been testing a java swing program that I have been making. On my one computer, the one I originally made it on, it works fine. I have tested the same program on 3 different computers and it runs when I launch it out of the ide, but when I double click the jar I get a popup error window titled 'Java Virtual machien Launcher'. The error is "Could not find the main class: xxxxxxx. Program will exit."
I cannot figure out for the life of me what is going on. It was working before.

You need to include a Manifest file within your jar. In this, you specify which class is to be used as the entry point when the jar gets launched.
Create a file called Manifest.txt, and add:
Main-Class: yourMainClass.class
Then, to create the jar :
jar cfm JarName.jar Manifest.txt yourMainClass/*.class
To run the from the command line, use :
java -jar JarName.jar

Related

Multiple ways of creating executable jar not working

I'm trying to make a jar that the user can double-click to run (not a .exe just a jar that can be double-clicked). The problem is while this question has been asked many times none of the answers have worked.
I think the problem is not on my system, because other jar files on my system (that I didn't create) run fine. This makes me suspect I am doing something wrong.
First I tried this example.
However when I tried running the jar I made with that solution with:
java -jar HelloWorld.jar
I got the error: "no main manifest attribute in HelloWorld.jar"
I suspect the error is with my manifest file so here is is:
Manifest-Version: 1.0
Main-Class: main.Main
I also tried: "File --> Export --> RunnableJar" in eclipse but while that allowed me run to my jar from the command line when I tried double-clicking it I got the following message: "A Java Exception has occurred".
At this point, I don't know what to do to make my jar double-clickable.
What can I do to make my jar double-clickable?
Update: I used a new command:
jar cfe HelloWorld.jar main main.class
Now when I try to run this new jar from the command line with
java -jar HelloWorld.jar
I get the following error: "Could not find or load main class main"
If you did everything as described in that example, I think that there is no association with the *.jar file in the registry. Check it.
Also it maybe that two versions of Java installed.
Check Right-Click -> Open With. Java Runtime should be listed there.

Java - Executable Jars

I wrote a Java GUI program in Netbeans IDE 8.0.1 called SampleChat and used the 'clean and build' function to create a jar file.
I went to the 'dist' directory that Netbeans created and double-clicked the jar file it produced.
A cmd window opened with the following line:
"Error: could not find or load main class.
However, when I opened cmd, changed to the same 'dist' directory and typed the command:
java -jar "SampleChat.jar"
the program ran just fine.
Can I not use Netbeans to create jar files that execute on double-click?
I did not use any pre-existing jar files to complete the program, hence, there was no 'lib' directory or anything like that.
I intend to share programs from a USB stick so it is vital that the only requirement is a JVM, mouse and maybe some fingers to double-click it.
An awkward solution I came up with is to write a batch file that runs the program, but it requires the jar file to not be moved around - screw that.
So how do I make my programs double-click executable?
I guess you are missing to create a manifest file. Therefore it's unknown what your mainclass is. In your java command executed from a terminal you defined the mainclass to execute.
From the tasks you mentioned I guess you are using gradle to build your jar?
Here http://www.gradle.org/docs/current/userguide/java_plugin.html in chapter 23.14.1
an example of an manifest is given.
If you add attributes 'Main-Class': 'com.project.MainClass' it should work.
BR

Executable jar in eclipse is not runnable

After I finished making a program in java on eclipse, and decided to make a runnable jar I went through all the correct processes and created it. When I tried to run it later, I clicked on it, but nothing happened! Is this because I only have one class or do I need to do somthing else?
Ps. It runs perfectly fine in eclipse.
run it from command prompt, if it just outputs to console then you may not see anything by double click run java -jar xxxx.jar
Normally a JAR file have manifesto file attached to it which hold the class file which contains main method.If you have created your program in eclipse with main method in the selected class and created jar file than it won't make any issue.Anyways re-create it by simply clean and build,go through the dist folder in your project it will have an executable jar file ready for you.
For console application you wont be able to see the output directly on the windows screen but only on command prompt by running the file by java -jar jarfilename.Else any frame or swing application will run atomatically on windows screen by double click.
Use the command java -cp yourjar.jar com.example.Main where yourjar.jar is the name of your jar file and com.example.Main is the full name of the class containing your main method( including the package name)

Launch application Java

I made an application using bluetooth and so I had to use external libraries (bluecove). I am now looking to create a single executable to be able to give it to testers. I tested the executable JAR and it does not work, so I flip on a basic script.
I created a folder with all the java files inside and my external library and a folder with my pictures in it.
when I compile I have no error:
javac-cp bluecove-2.1.0.jar *. java
But in execution:
java -cp bluecove-2.1.0.jar Main
it gives me "error: Could not find or load main class Main"! I'm sur that my class contain my Main function Main.
What strange me is that in eclipse everything goes perfectly but not outside.
All those who have an idea and a hand to me is to thank in advance;)
java -cp "bluecove-2.1.0.jar;." Main
linux:
java -cp "bluecove-2.1.0.jar:." Main
Note the . which indicates the current directory, it must be also on the classpath so that your class Main can be loaded.

"Could not find the main class" error when running jar exported by Eclipse

I have a java project that works perfectly fine when running it from within Eclipse. When I try to export it to either a "JAR file" or "Runnable JAR file" the .jar file is created, but when I double click on it to try to run the program it gives me an error that says
"Could not find the main class: package.MainClassName. Program will exit."
As I mentioned, I tried exporting to both JAR options, I specified the correct class that the main method is in, and when I look through the actual files in the .jar file everything seems to be in order -- the manifest looks something like:
Manifest-Version: 1.0
Main-Class: package.MainClassName
(blank line)
and is in the META-INF folder. There is a folder with my package name, which contains all the .class files, including the class that contains the main method. A few image and text files that I use also appear in the jar file.
The actual program isn't anything too complicated -- it's a simple "snake" game using Swing (plus the code all works when run from inside Eclipse).
Any ideas what is causing this error and how I can fix it? Let me know if there's any other information I should provide.
Verify that you can start your application like that:
java -cp myjarfile.jar snake.Controller
I just read when I double click on it - this sounds like a configuration issue with your operating system. You're double-clicking the file on a windows explorer window? Try to run it from a console/terminal with the command
java -jar myjarfile.jar
Further Reading
Running JAR file on Windows
The manifest has to end with a new line. Please check your file, a missing new line will cause trouble.
Run it like this on the command line:
java -jar /path/to/your/jar/jarFile.jar
Ok, so I finally got it to work. If I use the JRE 6 instead of 7 everything works great. No idea why, but it works.
Had you tried creating a .jar file manually instead of using Eclipse. Try the following steps, hopefully that might help :
Considering that your directory structure looks like this :
TicTacToe(Your Project Name I mean)
| | |
src bin manifest.txt
| |
icons tictactoe
Now suppose that my main class is BeginGame inside package tictactoe, so I will write inside my manifest.txt file this thing :
Main-Class: tictactoe.BeginGame
Do remember the space between colons : and package name i.e. tictactoe, and immediately after BeginGame press Enter and save the file.
Now on your command prompt go to the location of bin folder, I am describing my side as follows :
C:\Mine\Eclipse\TicTacToe\bin>jar -cfm ..\tictactoe.jar ..\manifest.txt tictactoe icons
Here the first argument i.e. ..\tictactoe.jar is used to tell that create tictactoe.jar one level up, i.e. inside TicTacToe Folder.
Second argument ..\manifest.txt means that the manifest.txt file is located one level up, i.e. inside TicTacToe Folder.
Third and Fourth arguments tictactoe and icons means, that add both these folders to the .jar file, since they are placed inside bin Folder so they are used as it is. Now Press Enter.
Now try to run your .jar file so created inside the Project Folder (TicTacToe Folder, in my case).
Hopefully this will work.
Have you renamed your project/main class (e.g. through refactoring) ? If yes your Launch Configuration might be set up incorrectly (e.g. refering to the old main class or configuration). Even though the project name appears in the 'export runnable jar' dialog, a closer inspection might reveal an unmatched main class name.
Go to 'Properties->Run/Debug Settings' of your project and make sure your Launch Configuration (the same used to export runnable jar) is set to the right name of project AND your main class is set to name.space.of.your.project/YouMainClass.
I ran into the same issues the other day and it took me days to make it work.
The error message was "Could not find the main class", but I can run the executable jar exported from Eclipse in other Windows machines without any problem.
The solution was to install both x64 and x86 version of the same version of JRE. The path environment variable was pointed to the x64 version. No idea why, but it worked for me.
Right click on the project. Go to properties. Click on Run/Debug Settings. Now delete the run config of your main class that you are trying to run.
Now, when you hit run again, things would work just fine.
For netbeans user that having this problem is as simply:
1.Go to your Project and Right Click and Select Properties
2.Click Run and also click browser.
3.Select your frames you want to first appear.
you are just missing static keyword. that's all.

Categories

Resources