axesspwns
axesspwns

Reputation: 113

Compile java classes protobuf

I am having trouble with compiling the AddPerson.java class for the Protubuf tutorial on Google while following a youtube video link below at 5:09. I keep getting addperson command not foundenter image description here https://www.youtube.com/watch?v=BJqSDvCZcx8&t=312s

This is the command that I used.

java -cp protobuf-java-3.5.1.jar; AddPerson addressbook.data

I'm currently on Ubuntu linux running the command in the terminal. I have a feeling the command might need to be slightly tweak for linux as the op of the video is using Windows with the command prompt.

Upvotes: 0

Views: 1852

Answers (3)

Kaviranga
Kaviranga

Reputation: 666

When you want to run a java console app or other *.jar file with help of external jar file . This the procedure ,

In Linux Environment

compile

javac -cp .:/home/user/ess_jar_files/protobuf-java-3.5.1.jar AddPerson.java

run

java -cp .:/home/user/ess_jar_files/protobuf-java-3.5.1.jar AddPerson addressbook.data

In Windows Environment

compile

javac -cp .;D:\essential_jars\protobuf-java-3.5.1.jar AddPerson.java

run

java -cp .;D:\essential_jars\protobuf-java-3.5.1.jar AddPerson addressbook.data

Upvotes: 1

Stephen C
Stephen C

Reputation: 718678

There are two distinct problems.

  1. Since you are running on Linux, the class path separator is : rather than ; as shown in the video.

  2. You need to include the JAR file or directory containing >>your<< code on the classpath. I expect that the video actually did this using the . character. Remember that on Windows AND Linux, . means "the current directory".

So you should be running the command on Linux like this:

$ java -cp .:protobuf-java-3.5.1.jar AddPerson addressbook.data

This assumes that your AddPerson class does not have a package declaration, and that AddPerson.class is in the current directory. It gets a bit more complicated if packages are involved.

Note that I put . in front of protobuf-java-3.5.1.jar on the classpath. It probably doesn't matter in this case, but in general the order does matter. The classloader searches for classes in a specific order1.

  • first, the system classpath where the standard library classes live
  • then, the Java installation's Extensions directory2
  • finally, the locations in the application's classpath ... in the order that they are specified on the classpath.

The first one class with the correct (fully qualified) name that it finds will "win". So if your classpath was -cp protobuf-java-3.5.1.jar:. and there just happened to be3 an AddPerson class in protobuf-java-3.5.1.jar, then that would be loaded in preference to your class.


1 - This can be further tweaked by the application itself creating classloaders at runtime. But that is a different topic.
2 - For Java 8 and earlier. Support for the "extensions" mechanism was dropped in Java 9.
3 - Don't worry, there isn't. But the point is that the classpath order can matter.

Upvotes: 1

axesspwns
axesspwns

Reputation: 113

After more research, you use .: before the name of .jar file instead of the semicolon at the end.

java -cp .:protobuf-java-3.5.1.jar AddPerson addressbook.data

Upvotes: 1

Related Questions