Creating and running a Jar file - java

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.

Related

How do you export multiple java files as a singular runnable jar file in Eclipse?

I have a project with 3 java files:
Title_Screen .java
Game_Screen .java
Game_Code .java
Game_Screen and Game_Code use OpenGL, and Title_Screen opens Game_Code which opens Game_Screen. In eclipse, the program works perfectly, but when I try to export it as a runnable jar file, no matter what I do, it always just exports Title_Screen.java. What am I doing wrong, and what steps do I have to take to export all three java files in one .jar file?
Edit: It seems to only happen to my program, perhaps it's something to do with the OpenGL libraries?
Edit 2: I removed the libraries from my program, same results as exporting it to a jar file. My actual problem is that I can't put in the libraries.
Edit 3: Problem resolved! All I had to do was use jarsplice to create my runnable jar, not Eclipse. Tutorial I used: https://www.youtube.com/watch?v=YqGUk84BmlQ
You can use the following command to build the jar and specify the main class entry point (Main).
jar cfe output.jar Main src/Repository/* src/util/*.class
you can write multiple files when creating the jar
jar cf Output.jar src/util/Main.class src/util/SubMain.class src/Repository/*
Or from eclipse
Put all your files in a folder in your Eclipse project and then:
1.Right click in your folder
2.Export
3.Java -> Runnable JAR File
What steps are you following to export it as runnable jar?
Are you able to run the jar file successfully?
Steps to export Runnable jar is :
Right click on project and select "Export" option.
Provide the destination path where the jar file needs to be store.
Export

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

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

Run Java project (built in eclipse) using Batch File

I want to run my java project which i've built using Eclipse IDE. Now my goal is to create a batch file which will execute my project with one click. i referred question , but didn't get any idea. Please give me a solution. Thanks in advance.
Because you are working in eclipse, this makes things easier. First, export the whole program as a executable jar. You can do this by going to Files>Export and then in the pop up go to Java>Runnable Jar. Then follow the steps required to make it. Next you make a .bat file and write the following code.
start javaw -jar NameOfJar.jar
Make sure that you put the file in the same directory as your jar. Now you should be able and click on the .bat and execute the program!
1.Open Notepad & write
#echo off
javac YOUR_JAVA_FILENAME.java
java YOUR_JAVA_FILENAME
2.Save-As executeJavaProgram.bat
Make sure that YOUR_JAVA_FILENAME.java resides with your batch file and java path is set in environment variable.
3 . Double click on batch file.
Follow this tutorial to create a jar file of your eclipse project.
After doing it create a batch file in the same folder where you exported the jar with the command: java -jar yourjar.jar
Create jar file using eclipse i.e right click on your project select export jar file then provide file name to store your jar file. In this file eclipse will keep all .class file only and in META-INF folder main class definition if your are creating executable jar file.

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.

Categories

Resources