8

I am new to Java (basically a LAMP developer). I got this JAVA API to parse .pst files and show all the inbox messages.

I tried executing a given .class file but it throws exceptions. I need to add/reference some .jar files provided by the API.

I don't have any IDE for Java yet. Wikihow says

When your Java project requires JAR libraries to function, you have to configure your project to include the libraries in its build path. Fortunately, Eclipse makes this process simple and easy to remember. The build used here is Eclipse Java - Ganymede 3.4.0.

So, what configuration do I have to do? Or is it better to get Eclipse IDE? I just have a single .class file to be executed.

A few other questions that I checked but could not get my answer - How to add external jar libraries to an android project from the command line

How do I include external JARs in my own Project JAR

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • 1
    You should really get Eclipse or Netbeans to help you program. The IDEs in Java are really gooood compared to what you have for PHP! – Alfred Jan 19 '11 at 07:54

2 Answers2

12

You should put them on your classpath, like

java -classpath someJar.jar YourMainClass

And, of course, you can do the same for javac.

If you need to have more than one jar or directory on your classpath, you'll need to use your platform's default path separator. For example, on Windows,

java -classpath someJar.jar;myJar.jar YourMainClass

On a side note, you might find it easier to use an IDE to manage this sort of stuff. I've personally used just my slightly scriptable editor and have managed fine. But it's good to know how to do this stuff by command line.

Zach L
  • 16,072
  • 4
  • 38
  • 39
  • 3
    And make sure you use the fully qualified class name. Chances are that your main class is more something like com.myco.bubu.YourMainClass – Jochen Bedersdorfer Jan 19 '11 at 07:58
  • 1
    Thanks for the answer Zach L. I am still struggling with the executing the `.class` file. As per the API people, only one `.jar` file needs to be referenced. Even after adding `classpath` to the compile and execute commands I am getting exception in the execute command - `Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example ...`. Any pointers? The compile command works fine producing the .class file – Sandeepan Nath Jan 19 '11 at 10:21
  • I know it is difficult to answer this without looking at the code but I only need to be sure that I have done the correct adding/referencing of the .jar dependencies. I did `javac -classpath /opt/Java/libs/JPST/lib/jpst.jar;/opt/Java/libs/JPST/lib/mail.jar /opt/Java/libs/JPST/examples/GetInboxMessages/src/Example.java` and then `java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example` If this is correct then the problem is probably something else. – Sandeepan Nath Jan 19 '11 at 10:25
  • I'm not sure, but is the last part the name of the class? You don't need to put the whole path in front of the class. You just put the name of the **package** it belongs to (like what Jochen says), like `java -classpath /opt/.../jpst.jar nameofpackage.Example – Zach L Jan 19 '11 at 16:01
  • this did not solve my problem. I only have one .JAR file i need referenced, though the Reference error persists... Strange that compiling yields no error whatsoever, running yields the reference error. Basically, i have the same problem as @SandeepanNath – Michahell May 14 '13 at 09:43
6
javac -cp yourjar.jar YourClass.java

&

java -cp yourjar.jar YourClass

You need to make all required jar available in classpath , this is how you can do it

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    life.java: +1. I am still struggling with the executing the `.class` file. As per the API people, only one `.jar` file needs to be referenced. Even after adding `classpath` to the compile and execute commands (like you said), I am getting exception like `Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example ...`. Any pointers? – Sandeepan Nath Jan 19 '11 at 10:26
  • I know it is difficult to answer this without looking at the code but I only need to be sure that I have done the correct adding/referencing of the .jar dependencies. I did `javac -classpath /opt/Java/libs/JPST/lib/jpst.jar;/opt/Java/libs/JPST/lib/mail.jar /opt/Java/libs/JPST/examples/GetInboxMessages/src/Example.java` and then `java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example`. If this is correct then the problem is probably something else – Sandeepan Nath Jan 19 '11 at 10:27