• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

3rd party library problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "-cp" option does something different than the "-jar" option.
 
rosy Jovita
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ulf,

what do you mean by that?
do you mean, compile the program using javac -jar sigar.jar MemoryMonitor.java?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have tried to run using this:
java MemoryMonitor
then got this error:

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
rosy Jovita
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you try to delete and recompile the code?
 
rosy Jovita
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, what i mean is i'll delete the package only.
thanks
 
rosy Jovita
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
rosy Jovita
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my class name is MemoryMonitor.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

thanks for helps:D
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:D
Thanks to javaRanch members and developers.
reply
    Bookmark Topic Watch Topic
  • New Topic