Java: Adding included Jar files to a created Jar file in linux - java

I need help including imported jar files into my java program in Linux. Here is the program:
import java.sql.*;
public class CreateCoffees
{
public static void main(String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
System.exit(1);
}
}
}
In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath:
db2jcc_license_cu.jar
db2jcc4.jar
I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this:
javac CreateCoffees.java
java CreateCoffees
But I got this error
ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
Then I tried the "-classpath" option
javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees
but got this
Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees
How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ?

Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly

How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.

Related

CMake add_jar with INCLUDE_JARS not working

I am using CMake to compile jar file with add_jar command. Problem is that when I try to add INCLUDE_JARS to specify dependency to external jar, the code will not run. Here is the code example:
add_jar(testJar
SOURCES
sources/com/test/Main.java
INCLUDE_JARS
${CMAKE_SOURCE_DIR}/extern/org.json/json-20171018.jar
ENTRY_POINT com.test.Main
)
Running the testJar with "java -jar testJar.jar" gives me the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
The org.json jar should be in classpath, right? Adding manifest file with classpath solves the problem but is there way to do this without the manifest file?
And yes, I would use maven or gradle for building but as of restrictions in the project I cannot do that :)
The option INCLUDE_JARS only makes sure that the given external jar is added to the class path upon compilation of the given Java source files. The jar file is neither copied to the build directory nor is it added to the jar file generated by add_jar.
If inclusion of the external jar in the Manifest file is not an option for you, manually add it to the class path upon running the testJar, i.e.:
java -cp path/to/json-20171018.jar -jar testJar.jar

NoClassDefFoundError when runnable jar

Hi Guys I have included the Webcam-Capture API in my project.
When I run it in Netbeans everything works fine. But when i compile everything to a runnable jar i get this message trying to run it by cmd line.
can anyone of you help me?
i already tried to unbound and rebound all jars and changing jdks but its not working
add -classpath flag in the command line ,pointing to the path where Webcam-Capture API exists in your file system, unless you want to create a single package executable.In your case It should be something like below
java -classpath YOURJAR.jar;folder_of_dependant_jar/*;. com.awesome.pagackage.Starter
Where YOURJAR.jar contains the com.awesome.pagackage.Starter.main(String args[])
You also mentioned that your jar is a runnable jar it also means that while exporting/building you can do one of the following way.( NOTE , this feature is in eclipse , but you would get the idea ).Each of the following options you see in the library handling does specific things.
The first option: Extracts the dependent jar into your target jar as java packaging.This means if your package is com.awesome.package and the dependent jar has package logic.package; , after the runnable jar is build you could find both these package exists in your jar file.
The second option: I think it is more on eclipse specific since eclipse adds few classes of its own , of runnable generation, so I am not explaining it here.
The third option : is the most interesting one. it creates folder stucture like below
ndon_lib\external.jar ( external jar file )
ndon.jar ( your jar file )
This time the manifest.mf file contains something like below.
Class-Path: . ndon_lib/external.jar
Main-Class: com.awesome.pagackage.Starter
You should set the classpath
java -cp "your.jar" "yourclass"

Using JAR Files

So I'm still a noob in Java and I'm experimenting around with a few things.
I recently created a .jar file for my class using jar cvf <name>.jar <source files> and then used that jar to compile my driver class (javac -cp <name>.jar Driver.java) though how do I now run that class using the jar?
I've tried the following 2 commands:
java Driver and,
java -cp <name>.jar Driver.
The first gives me a NoClassDefFoundError for the class used, whereas the latter just gave me a single line error.
Error: Could not find or load man class Driver
What am I doing wrong? Is it possible I'm confusing this for something else?
I'm trying to do as much as I can without the use of any IDE.
You should put jar file and compiler output into classpath and specify main class:
java -classpath "<name.jar>;classes" Driver
EDIT (thanks to Kayaman):
If you are running command from linux/unix you have to use ":" as separator (in Windows works ";"). "classes" is a path to folder containing compiler output.
When creating an executable jar ( jar which contain a class with the main method) you should tell the jar which is the mainClass to be executed and for that you should create a file called 'Manifest.mf'.
The file should contain this:
Main-Class: MyPackage.MyClass
And when creating the jar you should use this to include your manifest:
jar cfm MyJar.jar Manifest.mf MyPackage/*.class
And for launching your jar :
java -jar MyJar.jar

Why does my program not find my java classes?

I have a project that I have compiled to a jar with Maven which I named H.jar.
The maven command I use is (in eclipse):
maven install
The jar file has a class called Person in it.
I have added the jar file to the classpath on a Windows machine.
echo %classpath%
Results -> C:\location_to_jar\H.jar
But when I try to compile the program I get error:
error. Cannot find symbol.
I am running the command:
javac Main.java
The class looks like this:
public class Main {
public static void main(String[] args) {
Person p = new Person("John", "Doe");
p.toString();
}
}
Should the program not just find the class if I have added it to the classpath?
It sounds like your program is fully contained within a single jar, with no external dependencies, so I would recommend that you avoid using the %CLASSPATH% environment variable. Instead, point Java at the jar directly.
Assuming that your Maven build has generated an executable jar with your Main class defined as its entry point then you can just execute:
java -jar C:\location_to_jar\H.jar
If the executable class has not been defined in your build, then you can define the classpath on the command line:
java -cp C:\location_to_jar\H.jar com.mypackage.Main
You will need to replace the package in the command above with the appropriate package name as defined in your application.
If you want Maven to make your jar executable, then you can use the Maven Assembly Plugin to help. An answer which explains that can be found here: How can I create an executable JAR with dependencies using Maven?

running jar file from my class file in the IDE netbeans

I use a jar file called korat.jar. I executed with the command line:
java -cp $CLASSPATH korat.Korat --visualize --class test.Add --args 3
The classpath contains the path of the jar and also the Add.class file.
I want to execute this jar in my own program java in netbeans IDE. I think I would use:
String []s={test.Add.class.getName(),"--visualize","--class","test.Add","--args","3"};
Korat.main(s);
I get this exception: java.lang.NoClassDefFoundError
What do you mean "to execute the jar in my own program"? The jar contains some classes, if they are in your class path, you can instantiate the classes themselves and invoke some methods. In that case, you should use test.Add class. But it seems like the class is not in your classpath - java.lang.NoClassDefFoundError.

Categories

Resources