1

I have a Raspberry Pi Type Bi with Oracle java installed.

Previously I have been able to run java classes e.g. "java MemoryInfo" and the file would run.

Now I want to include a separate library and cant seem to get it to run. The java files and library run fine in Netbeans.

I am running the command "java -classpath sigar.jar CpuInfo" and is says could not find or load main class. I know the code is correct as it runs on netbeans.

Interestingly I can run "java -classpath sigar.jar MemoryInfo" even though the library is not required, it says the same about not being able to find the main class.

I have the library (sigar.jar) and the class files in the same directory (/home/pi). What am I missing? Help!


I have tried with another java file. It compiles ok with: "javac -cp avis-client.jar HelloWorld.java" but I get the same error when I try to run: "java -cp avis-client.jar HelloWorld"

The exact wording is: "Error: Could not find or load main class HelloWorld"

The location of the jar file is ok as it wouldn't compile without it!

3
  • 1
    Is CpuInfo the fully qualified class name, including the package? Does it say it can't find a main class, or can't find the CpuInfo class? Or can't find the main method of the CpuInfo class?
    – francis
    Commented Feb 5, 2014 at 14:31
  • I have now tried it with another java file that needs a library and that doesn't work either. The exact wording is "Could not find or load main class HelloWorld. I have updated my question with more detail. Commented Feb 5, 2014 at 15:27
  • But you want to run the class CpuInfo, not HelloWorld! Are you sure you're not typing java -jar sigar.jar CpuInfo ? (I should be clearer, -cp is correct, -jar would be wrong in this case)
    – francis
    Commented Feb 5, 2014 at 15:32

2 Answers 2

1

Ah, the class you want to run is not in the jar! Then add the current directory . to your classpath as well as the jar.

java -cp sigar.jar:. CpuInfo

Also it's good to get into the habit of using your own packages, rather than just using the default package.

====

Question now updated with even more examples...

From the home directory /home/pi, you should have the files MemoryInfo.class, CpuInfo.class and sigar.jar, right? Please check this (including getting capitals and little letters correct on all the filenames).

From what you said, java MemoryInfo works, right? So try java -cp . MemoryInfo and that should work too. This just specifies the current directory as the classpath. Now try java -cp sigar.jar:. MemoryInfo and that must work too, because adding sigar.jar and . to the classpath shouldn't stop things working.

Now if that works, and java -cp sigar.jar:. CpuInfo doesn't work, then that would be really strange indeed, unless sigar.jar depends on other jars too?

Also, what platform is your netbeans on? Can you run CpuInfo from the command line on that machine or not? I'd be really really surprised if this was a Raspberry Pi problem.

3
  • I created a jar file of the project in netbeans and used the full paths and it still doesnt work :-( the command I used was: sudo java -cp /home/pi/sigar.jar:/home/pi/JavaApplication1.jar javaapplication1.CpuInfo.main Commented Feb 5, 2014 at 16:38
  • why .main ? Try java -cp ~/sigar.jar:~/JavaApplication1.jar javaapplication1.CpuInfo
    – francis
    Commented Feb 5, 2014 at 18:30
  • tried that as well, its not going well for me haha :-( Commented Feb 5, 2014 at 18:56
0

I had this issue and solved it by doing the following:

  1. Be sure to run your program from within the working directory of the Java class that has your desired main()
  2. Be sure to specify the locations of the JAR parameters in your -classpath correctly (use directories, separate with colons)
  3. Use the full name of the class that has main()

For example, if you have the following files:

/opt/foo/Bar.jar
/opt/foo/lib/Baz.jar
/etc/Bam.jar

And com.example.MainClass contains the class with main() and lives in Bar.jar , then you want to do:

$ cd /opt/foo
$ java -classpath Bar.jar:lib/Baz.jar:/etc/Bam.jar com.example.MainClass

As a side note, you can substitute -classpath with -cp like so:

$ java -cp Bar.jar:lib/Baz.jar:/etc/Bam.jar com.example.MainClass

Parts of this advice may not be strictly necessary, but I haven't tried all possible combinations and this has worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.