File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes 3rd party library problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "3rd party library problem" Watch "3rd party library problem" New topic
Author

3rd party library problem

rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
Hi guys,

I don't know where else i could ask about this, so I simply posted it here. Hope someone can help me.
i was trying to run sigar on ubuntu. Have successfully run it on windows but having problem with ubuntu.
In windows, i just simply copied the sigar.jar and other important library into the jdk library but in ubuntu I've tried to do this but it doesn't works.
It still can't find the library.
what should i do?I even tried this command: javac -cp sigar.jar MemoryMonitor.java
compilation works but when i tried to run using this command: java -jar sigar.jar MemoryMonitor
it gave me this: command not found: MemoryMonitor.

Please help
thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
The "-cp" option does something different than the "-jar" option.


Ping & DNS - my free Android networking tools app
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
ulf,

what do you mean by that?
do you mean, compile the program using javac -jar sigar.jar MemoryMonitor.java?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
What I mean is that you're trying to use -cp and -jar interchangeably for adding a jar file to the classpath, but only one of two actually does that; the other does something completely different, which leads to the problem you described.
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
okay, then what should i do?
please help me, i've dealing with this problem for month.
my friends get it works on his pc using this command.
my os is ubuntu.
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
have tried to run using this:
java MemoryMonitor
then got this error:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Apparently that class has a package statement; do you know what that means? Amongst other things, it implies that it needs to be in a particular directory path that mirrors the classpath.

An alternative approach would be to remove the package statement.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

1. Don't use the "-jar" option to include a third-party library. Use the "-cp" option for that. The "-jar" option is for running executable JAR files.

2. When you use the java command to run a program, you must specify the fully qualified class name, i.e. the name of the class including the package that it's in. If the class MemoryMonitor is in package org.hyperic.sigar.cmd, then you have to put that in front of the class name:

java -cp sigar.jar:. org.hyperic.sigar.cmd.MemoryMonitor

Note: the ":." is to add the current directory to the classpath.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
ulf, yeah, i included this package org.hyperic.sigar.cmd.
okay, i'll try to delete and recompile the code. thanks

jesper,

okay, i'll try that. Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why would you try to delete and recompile the code?
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
No, what i mean is i'll delete the package only.
thanks
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
and now, i've tried all that.
got his error:

Exception in thread "main" java.lang.NoClassDefFoundError: MemoryMonitor/java
Caused by: java.lang.ClassNotFoundException: MemoryMonitor.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MemoryMonitor.java. Program will exit.

what else i missed out.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40


It's looking for a class named "java", in the "MemoryMonitor" package. Did you really named your main class "java"?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
my class name is MemoryMonitor.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Then you're running it incorrectly, probably by entering "java MemoryMonitor.java" on the command line. Development is all about paying attention to details :-)
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
ulf,

i've tried all that.
have a look at this:

rose@rose-desktop:~$ javac -cp sigar.jar MemoryMonitor.java
rose@rose-desktop:~$ java MemoryMonitor
Exception in thread "main" java.lang.NoClassDefFoundError: org/hyperic/sigar/SigarException
Caused by: java.lang.ClassNotFoundException: org.hyperic.sigar.SigarException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MemoryMonitor. Program will exit.


again, another error.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
How do you expect a program to work if you don't supply it with the required libraries? You need to do that during compilation as well as during running the code.
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
ulf,

i have tried everything.
look at this:

rose@rose-desktop:~$ java -cp sigar.jar MemoryMonitor
Exception in thread "main" java.lang.NoClassDefFoundError: MemoryMonitor
Caused by: java.lang.ClassNotFoundException: MemoryMonitor
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MemoryMonitor. Program will exit.

i am really out of idea.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
The problem is that the classpath needs to contain *all* required classes. Right now, only the library is in the classpath, but not the code you wrote. Assuming that MemoryMonitor.class is in the current directory, then "java -cp sigar.jar:. MemoryMonitor" will do the trick (note that "." denotes the current directory, as is generally the case in Unix-based operating systems).

I suggest you get a good introductory book that explains these concepts; that's much more efficient for getting a solid grounding than randomly trying various things.
rosy Jovita
Ranch Hand

Joined: Jan 05, 2010
Posts: 45
Hi all,

thanks for helps
Thank you very much.
i get it worked now using this command: javac -cp ./sigar.jar:. MemoryMonitor.java

Hope this also help those who face the same problem
Thanks to javaRanch members and developers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 3rd party library problem