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.
Related
I have made use of JDBC in one of my projects and it worked flawlessly in Eclipse. I had to add the ojdbc6.jar to Java Build Path to the project BTW. (I have also not included any Class.forName statements in the code since I heard it's optional for JDBC 4 and above)
When I export the project into a jar file, and try to run it, it complains saying "No driver found for JDBC" and so on. I can see that this is because the ojdbc6.jar is missing from the build path now. Can someone please let me know of a solution?
Thanks!
Here is a simple method to export a JAR that uses external jar as libs:
You have to write a manifest file, I named it jar.manifest.
The content in manifest file looks like:
Manifest-Version:1.0
Main-Class:com.enginebai.activity.MyJDBCProgram
Class-Path:lib/ojdbc6.jar lib/some-externam-lib.jar
and make sure to put a blank line under Class-Path, otherwise the JAR won't run. (I have no idea why to put a blank line over there.)
Let's go back to eclipse:
In eclipse, you export your classes and remember to select above manifest file.
Make sure your ojdbc6.jar be in project/lib folder.
Open your terminal and cd to project root directory, enter `java -jar your-jar-name.jar', it should work then.
put ojdbc6.jar on classpath when you run your jar
I have done some research and found a solution that seems to work.
I have exported the project into a "Runnable Jar file" instead of just a "jar file".
This way, I was actually able to repack Jar files from my Build Path into the new Jar file I am exporting my project to. Simple and clean.
You also might want to look at signed Jar files and any other certificates in your existing jar files in the Build Path. They cannot directly be repacked through the export option.
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
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.
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.
My project runs fine from Eclipse.
But when I tried to make it into a jar file or executable file it doesn't work.
I used the option "Export-Runnable JAR file"
The following message appears just after the eclipse finished the exporting process
JAR export finished with wornings , see details.
the details were ..
Exported with compile warnings:Mario/src/Map.java
and the same for other classes like
Exported with compile warnings:Mario/src/Player.java
and so on.
So that I used the other option "Export - JAR file"
It works fine and nothing appears while exporting it from Eclipse.
But when I try to open the file it gives me
Couldn't find the main class:Frame.Program will exit
Somebody have any idea about what the problem is?
Your MANIFEST.MF file inside the META-INF dir should have a Main-Class attribute pointing to your main class.
The important thing for executable jar is Manifest. Make sure it exists and points to the correct class with main method