Getting ClassNotFound Exception when executing jar - java

Am using IntelliJ 12.0.4 Community Edition.
I created a Java console app called DB with main class name of DB.
I packaged it into an executable jar file called DB.jar.
In that app I connect to an Oracle DB using JDBC.
I packaged the necessary JDBC jar files into the DB.jar via the Intellij's Project Structure (Modules, Library)
When I execute the app from within IntelliJ it runs successfully
If I copy DB.jar to some directory and execute it via "java -jar DB.jar" I get an ClassNotFound exception on oracle.jdbc.driver.OracleDriver
I looked in DB.jar and the jdbc jar files (ojdbc6.jar, ojdbc14.jar) are in DB.jar
Any thoughts?

Jars in an executable jar are not on the classpath, typically.
You can put them in the same folder as DB.jar and do:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar <MainClass goes here>
and that should run it.
You could also put the Class-Path entry in the Manifest.MF file inside your jar to reference other jars relative to the location of DB.jar in the computer's file system. Reference the two other jar files and then you can do java -jar DB.jar <MainClass goes here> (assuming the two jars are in the right place.
There is some discussion here about the general frustration about not being able to do what you want to do (and what many others have wanted for years).
Classpath including JAR within a JAR
Explain about Main Class
When you run a java application from the command line, there is some class where the execution starts. It will look something like this:
package com.mycompany.app;
public class StartHere {
public static void main(String[] args) {
// Your code goes here ...
}
}
If this example was the class where your application starts you would use a command line this to start it. This assumes you have the StartHere class in the right package location in a jar on the classpath:
java -cp DB.jar;ojdbc6.jar;ojdbc14.jar com.mycompany.app.StartHere
Notice that this allows you to have multiple applications in the same jar. Just make multiple classes with a main() method and run them with different starting classes shown on the command line.

Related

Call methods on class in exectuable jar file?

I've created a method in a Java class that does some encryption. This class utilizes methods from some external jar files. The requestor of this method has asked that I include my class and the external jar files in one jar file. I am using Eclipse for my IDE. As I understand it, I need to create an executable jar file in Eclipse, in order to package the external jar files in my own jar file. In my project, I also created a basic class with a main method in my project that simply calls my method mentioned above (to provide a "main class" for the run configuration for the executable jar). I exported my project to an executable jar file, and verified that it runs (java -jar jar-file-name) in a command prompt window. I created a second test project in Eclipse, and added my executable jar file to the build path. I created a simple class in this test project with a main method that calls the method mentioned above (on the class in my executable jar file). This compiled, so I exported this project to a normal jar file, and tried to run it (with java -classpath executable-jar-file-mentioned-above;test-project-jar-file class-name. This fails with a NoClassDefFoundError, referencing a class found in one of the external jar files.
What am I missing here? This is my first experience with an executable jar file. Is there anything special that needs to be done when calling methods on a class in an executable jar file?
There is nothing special about executable jar files. The only difference is that an executable jar file defines a main class (with a main() method) that will be started when you run java -jar ....
A jar file is in fact a plain zip file containing classes, resources and some additional meta-information.
When you run your jar file and get a ClassNotFoundException, this is an issue with you class path. So you are somehow missing classes, you depend on, located in your external lib.
There are two ways to fix your issue:
Add the library jar to the class path when you run your runnable jar
Repackage your executable jar to contain all the classes from the external library
I don't know what the Eclipse export functions do. But I would recommend taking a look at the exported jar file first using your favorite zip utility.
I would also recommend using a build tool like maven to create your jar. Here is an example how this can be done using the maven assembly plugin: https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

How to specify a runnable jar to use tomcat's lib for dependencies

I have made a java project in eclipse that's a runnable jar if I export it.
My structure of my project :
When I export my project as a runnable jar it asks me how I would like my dependencies to be packaged and I choose to put it in a lib folder. So when exporting it exports the runnable jar as well as creates a sub folder with my dependent jar files which I want to be only the 5 jars located in Referenced libraries.
This is the export window :
This creates my runnable jar and then a folder _lib with my other jars.
This works fine but as you see in the first image I also included the Tomcat lib library because some of the jar files in tomcat lib are used by my project.
The problem is that when I export, all the jar files in the tomcat lib get exported to my sub lib folder which I do not want.
Basically I want to deploy my runnable to a server witch will get the dependent jar files from the sub directory lib only those 5 jar files in image (Referenced Libraries). Tomcat is installed on the server so it should point to tomcats lib to get the rest of the jars.
Want my project to use the generated lib folder and then the tomcat lib.
Hope it makes sense what I am trying to ask.
Using Eclipse and java 1.7. developing on windows, deploying to linux running the application via command line on linux box.
What I have done to get this working is on the linux side to start the java runable class another way by specifying the classpath this will give the class you are trying to run the ability to first check the folders that you specify. So in my example instead of executing my app on the linux box with the following :
nohup java -jar /usr/local/P6/StockListShunter/StockListShunter.jar &
I did the following and specified the Class path lib that java should check for .jar file dependencies :
nohup java -classpath :/usr/mylib1/*:/usr/StockListShunter_lib/*:/usr/share/lib/*: stockListShunter.Shunter &
Notice I specified 3 libraries java should check when running my runable class, mylib1,StockListShunter_lib and lib.
After you specified the classpath you will see you need to specify your java class witch will be your main method class. In my example it is stockListShunter.Shunter where stockListShunter is my package name.
Just take note that you should make sure that the class you are trying to excecute is included in one of the jar files that you specified in the classpath otherwise it wont find the class and you will get an error : Error: Could not find or load main class stockListShunter.Shunter
So when you run the command the process will be started, my program is in a loop so the process will be alive until I kill it. If your program should be alive you can confirm it is running by running the following command.
ps aux | grep stockListShunter.Shunter
Where stockListShunter.Shunter is your main class name. This should display all the processes where the command has a stockListShunter.Shunter in it.
If you want to end the process like mine that's in a loop you can run the following command.
pkill -f 'java.*stockListShunter.Shunter'
Again where stockListShunter.Shunter is your main class name. This should kill your java process.
Hope this could help some one in the future.

JAVA, Batch file errors

I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:
java -classpath jflashplayer.jar;bin TestProgram
The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).
Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.
When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.
The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.
In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:
java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram
Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.

Why am I getting NoClassDefFoundError / ClassNotFoundException

I have been following this tutorial accordingly.
Up to running the FirstExample class in the command prompt is when it starts to freak out for some reason. After attempting to run the following command:
java FirstExample
I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: FirstExample
I understand that it can't find the FirstExample class due to the classpath (for some reason) so I executed the following command:
java -cp . FirstExample
And now it returns a new exception:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Now it can't find the JDBC Driver. This confuses me because for starters, I ran the exact same coding through Eclipse and it works as expected, and secondly, I went as far as to ensure that I execute the same class file that Eclipse is executing, and the command prompt still returns exceptions. I also went as far as to put the FirstExample file in a separate folder, just for the purpose of copying and pasting the MySQL Connector into the same folder, and I still get exceptions.
I just don't understand whats going on, can someone help me please?
Many thanks.
The file path to the connector is as followed:
C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar
Hope this helps.
For testing purposes, I have placed the FirstExample class under the following path:
C:\java
This confuses me because for starters, I ran the exact same coding through Eclipse and it works as expected
This is because in Eclipse you add the libraries to the Build Path, and it will use all the libraries specified there in the classpath automatically when running your project. This can be noted here:
In order for you to execute your project using third party libraries from command line tools, you should specify the libraries manually in your classpath explicitly:
java -cp <path/to/mysql_jar/goes/here>:. FirstExample
By your comment:
the path to the MySQL file is: C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar (...) I have placed the FirstExample class under C:\java
This should be the command line to use:
java -cp "C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar; ." FirstExample
Note that it is better to store all the third party libraries in a single folder within your project, usually called lib, and put a reference to there. Assuming your current folder has a lib folder and all the third party libraries are copied there, the command line would be:
java -cp "lib\*; ." FirstExample
Use the next example to add your jars to the classpath:
java -cp "jdbc.jar;lib/*" my.package.FirstExample
You need to have the class com.mysql.jdbc.Driver (and all the imported classes) in the classpath too.
You should download the jar (http://dev.mysql.com/downloads/connector/j/5.0.html) and add it to the classpath.
The ClassNotFound Exception rises when there is an issue with the class name that you have written in Class.forName() or if the package is not set to the classpath variable. Make sure that you have added the jar file to the classpath ( C:............\jarfilename.jar;).
This is applicable for any JDBC Driver and jar files. The .jar files that are added to the classpath will not be visible to IDEs, in this case, you need to add the jar files to buildpath (in eclipse) or you can also copy the jar files to ext folder available in the Java installation folder.
Also note that the jar files of the DB Softwares may vary based on the DB software version that you are using for example if you are using the Oracle 11g, you need ojdbc6.jar file, in other versions of Oracle the number changes like ojdbc14.jar etc.

Java run jar file & include external jar

Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?

Categories