i'm trying to export my Java Project into a runnable JAR file with eclipse but if i want to run the finished JAR file I always get the error
Java Virtual Machine Launcher
Could not find the main class: src.main. Program will exit.
src.main is my main class, if i open the jar with winrar this class is in the folder src in the jar file.
I export the Project like this:
Right click on the Project name
Export
Runnable JAR file -> Next
Launchconfig: my main class.
destination: ...\Desktop\asdf.jar
Library handling: Extract required libraries into generated JAR (I also tried all the others)
Finish
I get a Warning: "JAR export finished with warnings. .."
OK
The libraries are added like this:
Right click on the Project name
Properties
Java Build Path
Libraries
Add External JARs
And than all the libraries are together in one external folder.
What am I doing wrong?
Does src.Start have a proper "public static void main(String[] args)" method?
Are you trying to start the app like this:
"java -jar myjar.jar" or "java -cp myjar.jar src.start"
Also, are you building the manifest by hand, or letting eclipse do it? I don't remember ever seeing eclipse add a classpath entry in the manifest but I might have missed it.
On the "Jar Manifest Specification" panel during the export, just select "generate the manifest file" and fill in the "Main class:" entry in the input field.
The first thing that I recommend you: don't use src.main in the way that you are doing it right now. In Java usually we use a folder called src or src/main or src/main/java to put the sources. Then, to use this as a package and class name is not a good idea. Secondly; don't call your class main it is not a keyword in Java, you can create a class with this name but... it's not a good idea.
And the solution to your problem: you need to set up your Main-Class and your Class-Path in your manifest file and be sure about the structure of your package (your jar), take a look into this tutorial, it will help you.
Related
I'm using IBM's Relational Application Developer (RAD) as my IDE. I have one Java class in my project and I named it extract.java. I have couple of library files and resources files associated with my project. I exported my project as a jar file using IDE and ran it through command line on solaris environment using following query:
$java -jar myFile.jar
I get the following error:
Failed to load Main-Class manifest attribute from myFile.jar
I saw suggestions that manifest file needs to be updated. I'm not sure what does that mean since I'm new to java and any help will be great.
PS: There are no errors when I run class as Java Application from IDE on windows machine.
In order to make a JAR an Executable JAR within RAD, do the following steps:
Right click on the project and select Export...
Select Java > Runnable JAR file and click Next.
In the Launch Configuration field, select your Java class that contains the public static void main(String[] args) method.
Specify the destination location.
Lastly, define how the libraries, if any, in your project should be packaged in the JAR.
Click Finish.
This should create the Manifest file with the correct Main-class header. Run the JAR with the java -jar command to make sure it's working.
Hope this helps.
In your jar file, you need to add or modify the file /META-INF/MANIFEST.MF corresponding to the manifest of your jar to add the attribute Main-Class:
Main-Class: my.package.MyClass
Assuming that my.package.MyClass is the Fully Qualified Name of the class that has the main method to call when you launch your jar file with java -jar myFile.jar
I am a beginner in java programming. I have created a package called Comp Project which contains two classes....
ATM: It has various functions to carry out actions of atm screen.
Main: This class calls all the functions from ATM and executes them.
Now the program is running good and I have to create a jar file.
I created a Jar file with the main class selected as Main. But when I try to run it....it doesn't work. The problem is that i don't understand what creating a manifest is...I looked onto other posts but i did not understand anything.
Can anyone tell me the steps to follow for creating a proper Jar file and Running it?? I need both as I don't know where i am going wrong.
Update: I am using BlueJ.
Create a manifest.txt file.
Main-Class: fully.qualified.class.name of your main class
Uses Main-Class as the entry point of this Jar file, when you double
click on this Jar file, the “Example.class” main() method will be
launched.
And then on command line
jar -cvfm Example.jar manifest.txt your-package-structure/*.class
Hope this helps.
You can use jar cf yourCompAppJar.jar Main.class to create your jar file, if you have JDK 6 you can use the cfe to specify the entry point.
If you are using Eclipse :
Right-click the project > click Export > select "Runnable JAR file" under the Java folder > In the launch configuration select the class name > choose where to save your jar > finish.
I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.
I write a very simple java program with two classes: Business and Main.
I want to create a jar such that if I email it to someone they can:
run the program (i.e. run the jar)
open the jar to view the source code.
the code can run on mac or windows
I have been using IDEs for so long I have forgotten how to do this.
I am using netbeans 7.x
EDIT:
I found the following way on Netbeans:
properties > packaging > exclude from jar file :: delete **/*.java
But when I try to execute the jar using
java -jar mybusiness.jar
it says
no main manifest attribute, in mybusiness.jar
But note that my jar has a main class. Am I missing a manifest file?
You can export a JAR file that includes the source code using Netbeans:
Right click on the project and select properties
Build -> Packaging
Remove Java files from the excluded files. And select build jar after compiling
It will create the jar file that includes the source code if it successfully compiles.
Well surely an IDE can do this too?. Just make some text files and put the source into them and drag them into the ide's. It's java so it should automatically run on all platforms. I am not sure what the problem is here?
This link explains how do u create manifest file and how do you specify your main class in manifest file as its necessary for executing jar.
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.