Failed to load Main-Class manifest attribute from myFile.jar error - java

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

Related

Creating and running a Jar file

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.

Program runs fine in IDE but not as Jar file

I have exported a jar file that I want to run the console. The code compiles and runs correctly in eclipse but I am having an issue running it from the console.
To me it looks like the referenced jar's I added via built path in the Eclipse project file and not being added to the export. If that is the case, how do I ensure that they do? If not, what am I doing wrong?
When you export your source code's class files to a jar using eclipse, only the .class files of your source are exported! Hence your exported jar file doesn't contain the referenced jars you mentioned in eclipse! Due to this, the error occurs while executing from command prompt.
Solution:
Take all the jar files required to execute the program, store it in the same directory as you store the exported jar file. Now while executing the java command, provide all the jar file's names in classpath field as following:
java -classpath .;JAR1.jar;JAR2.jar MainClass
Once you do this, your problem should be resolved!
The dependencies need to be on the classpath, i.e. run like this:
java -cp <path_to_jar1>;<path_to_jar2> -jar ScrumTimeCaptureMaintenence.jar
When running from the command line make sure any dependencies are set on the class path by listing them in the -classpath parameter

creating and running jar file with source

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.

Export Java Project with external libraries using Eclipse

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.

creating jar file for application which in turn contains external jar file

Hie all ,
I want to create an jar file for a java application, and i am able create the jar file for it properly.i followed the below link to create the jar file .enter link description here
But the problem what i am facing is that in my application i added the external jar's like jna.jar , platform.jar and vlcj-1.2.0-javadoc.jar files to run my application successful. This application will run fine when i run in eclipse , but when i create the far file for the same application I'm getting the error like
No class defination found error.
jna native library missing.
so please will you tell me what is the actual problem and how do I solve it . and one more thing while creating the jar file I enabled the option "Export java source files and resources".
thanks in advance
Datta
You have to supply full class path when you are running your application, i.e. something like:
java -cp myapp.jar;jna.jar;platform.jar com.mycompany.MyMain
If you are using -jar option you should put all third party libraries to manifest.mf packaged into your jar under META-INF, i.e. add line like:
Class-Path: jna.jar platform.jar
to you manifest.
Pay attenition that you should use ; for windows command line, : for unix command line and space when writing class path in manifest.

Categories

Resources