1

I am writing a program that uses three classes and an external .jar library. I got the code to work on my windows machine, on IntelliJ, and am trying to get it running on my Raspberry Pi (Raspbian).

The three classes are called "CommHandler", "SocketHandler" and "ReadAndWrite". ReadAndWrite being the main classes that calls to the others.

The three classes and the .jar library are together in the same directory (home/pi/Final1). I have moved to the directory these are all under and used the code below to compile it all, no errors occur from doing this.

javac -cp jSerialCom-2.0.2.jar *.java 

The problem occurs when it comes to running the file, I have been using this to try and run it

java -cp jSerialCom-2.0.2.jar ReadAndWrite

This returns the error message as mentioned in the title

Error: Could not find or load main class ReadAndWrite 

I have tried explicitly stating the directory to get to the ReadAndWrite file all resulting in the same error message. (Shown below, tried with both "/" and ".")

java -cp jSerialCom-2.0.2.jar home/pi/Final1/ReadAndWrite

If you have any idea how why this may be happening/how to solve it that would be great.

Thanks

P.S. I have looked at other questions but cant seem to solve my problem from them

e.g.1 - Error: Could not find or load main class

e.g.2 - Java command line with external .jar

Blenners
  • 23
  • 4
  • The [answer](https://stackoverflow.com/a/7485781/5221149) in the first link *you* provided says: *"You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then **add `.` to your classpath**"*. You didn't, so perhaps you *looked* at it, but you need to do more than just *look*, i.e. you have to actually change something in what you do, according to the recommendation given in the link. – Andreas Apr 20 '18 at 16:14

1 Answers1

2

java -cp overwrites the classpath. Add the current directory to the classpath so java can find the newly created class:

java -cp jSerialCom-2.0.2.jar:. ReadAndWrite
choroba
  • 231,213
  • 25
  • 204
  • 289