File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes import jar into the class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import jar into the class" Watch "import jar into the class" New topic

import jar into the class

moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
I have to create a program (prefer in Java since it is multi platform), that can run independently, read a file, and write something to a database.
I use H2 DB and I develope with Eclipse.
I developed a little class which does all I need. The class knows how to work with the H2 DB because I inserted the appropriate jar file of the DB
to the "build path" in the Eclipse.
Now, while running the class in command line thru "java class_name" , I get an exception because the jar of the DB is not exsist.
I guess I missed something. Anyways, the main goal is to run it out of the eclypse - in any place I'll put it....

I'll be glad to get some help... Thanks a lot , Moshi.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Welcome to JavaRanch!

When you run the program, you must also put the JAR file in the classpath. You can do that by using the "-cp" switch on the command line, for example:

java -cp filename.jar;. com.mypackage.MyMainClass

If you don't want your users to type this all the time, you can make a small Windows batch file in which you put this command.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
First of all thanks for your answer...
well, my main goal is that my java program will sit on a few computers, and be available to external calls.
Other sources will call my class that does something. I guess that for calling it they will call my class name
or something, it will be thru httprequest. (As you can see, since it is very new to me I'm not sure exactly how they will call it...).
But in 1 thing I'm sure - it will have to know the H2 jar anyways. How ?
Thanks a lot for any answer !
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Well, I think I have the answer:
I moved the jar file into same folder of the java class.
Then , I called it like this: java -cp filename.jar; myClass,
like you recommendeed and it worked.
The answer is how to make out of it a single unit that I can invoke
from outside, to send it params and to get from it an answer...
If someone has an isea....
thanks !
Abhi Kumar

Joined: Sep 11, 2008
Posts: 24
add your jar file to your system environment variables ...
Swastik Dey

Joined: Jan 08, 2009
Posts: 1579

You probably need to make an executable jar, which will include your classes, required jars and manifest file containing the information about main class and needed classpath.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
Abhi Kumar wrote:add your jar file to your system environment variables ...
That is not a good suggestion; your environment variables will gradually become cluttered with jars you no longer use, and you won't know which of them you can keep or delete.
I agree. Here's the link:
subject: import jar into the class
It's not a secret anymore!