2

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 ?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

  1. Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.

    enter image description here

  2. 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.

  3. 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.

  4. In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:

    enter image description here

    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.

  5. Then go to the terminal and look at the ant script:

    enter image description here

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.