-1

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.

Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.

So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get

java.lang.NoClassDefFoundError: 

Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:

javac -classpath ./path/to/jar Main.java java -classpath ./path/to/jar:. Main 

And this works.

So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?

java -cp ???(dont know what to put here) 

But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?

2
  • You are already asked the similar question. Did you try to put library to classpath of app server, as I mentioned here: stackoverflow.com/a/16149435/1430055 ? Commented Apr 23, 2013 at 17:10
  • And how shall you invoke methods on other library classes which are not present in the other computer, without copying the jar files to other computer? Commented Apr 23, 2013 at 17:11

3 Answers 3

0

I like to use something like the following:

java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass

This adds not only my jar to the classpath but those in the lib directory as well.

If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?

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

1 Comment

This might be very silly, but I am having trouble using the -cp command. I used your command. But it threw me an error. It says permission denied
0

To get your program to run you have two paths to worry about

  1. The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
  2. The path to the directory containing the classes of your app (lets call this classes)

The general form of the command line you need is:

java -cp lib/jsoup.jar:classes Main 

If you have more libs

java -cp lib/jsoup.jar:lib/jxl.jar:classes Main 

A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html

Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/

5 Comments

First thing I need to do is to be able to run it. Which I am having trouble using the java -cp
I am using: java -cp ../lib/jxl-2.6.jar:classes Main but it says Error: Could not find or load main class Main
What directory is Main.class in?
If we are on the root of the project folder, we have /bin /lib /src and Main.class is in /bin. I am executing this command inside /bin
ok. "classes" is meant to be the directory in which Main resides. So you need java -cp ../lib/jxl-2.6.jar:. Main if you are running from bin. If you are in the root then java -cp lib/jxl-2.6.jar:bin Main
0

if you have this structure:

project folder ... code ... libs 

then from the code folder:

javac -cp .;../libs/*.jar yourmainclass.java java -cp .;../libs/*.jar yourmainclass 

When you need to compile and run this project, take all the folder and do the same in other machine.

2 Comments

Ok I am in the code folder, and executed what you wrote but I am getting an error. Very Strange. Want to post it here?
This is what I get: alessandro@ALC:~/Java/Trabajo/src$ javac -cp .;../lib/*.jar Main.java javac: no source files Usage: javac <options> <source files> use -help for a list of possible options bash: ../lib/jxl-2.6.jar: Permission denied

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.