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.
Related
Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation
So I have a small Java program with some gui that I have runnning in eclipse just fine. I followed the fatjar tutorial to create the .jar, but when I try to run the .jar the first gui window appears but none of the functions work. When I ran it in cp, clicking the button generated an exception basically saying some of the object types could not be resolved as type, all of which were from import libraries. Has anyone seen this before, I tried using this program jarfix for an issue similar to mine, but nothing.
Please define
When I ran it in cp
The format of the java command should look something like
java -cp /path/to/jars com.main.class
You could start locating the source of the problem by opening the jar file with an archive program (e.g. winzip) and look if the class / type is included in the jar file or not.
If it is there it might be a classpath issue. If it is not there something is wrong with building the jar file. Musn't the included jar file be set in the "Order and Export" tab in the java buildpath dialog?
I am not aware of fatjar. But I faced similar problem, might be somewhere it is linked to your issue.
In my case everything was working when I was running through eclipse, but when I created JAR using Eclipse I faced issue as what you are facing, but in my case it was issue of accessing resources inside JAR File.
So solution was,
Right Click -> Export as -> Jar, here check the box "Add Directory Entries" and then create the JAR. Ans all worked.
Above all, first you should check whether the jar which is generate is correct or not by just open it with winrar or similar tool and see all classes and resources are placed properly.
An alternative would to place all those jar files in the same folder. i.e. if your application jar file is App.jar and other dependencies are A.jar and B.jar. Then drop A.jar and B.jar in the same folder as App.jar.
If you double click on App.jar it will by taking the other dependencies in the same folder by default.
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.
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
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth