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: 35241
7
posted
0
The "-cp" option does something different than the "-jar" option.
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: 35241
7
posted
0
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
posted
0
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
posted
0
have tried to run using this:
java MemoryMonitor
then got this error:
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
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.
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:
Why would you try to delete and recompile the code?
rosy Jovita
Ranch Hand
Joined: Jan 05, 2010
Posts: 45
posted
0
No, what i mean is i'll delete the package only.
thanks
rosy Jovita
Ranch Hand
Joined: Jan 05, 2010
Posts: 45
posted
0
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.
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
posted
0
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: 35241
7
posted
0
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
posted
0
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: 35241
7
posted
0
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
posted
0
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.