编译 Java 类的 Protocol Buffers 文件。

huangapple go评论79阅读模式
英文:

Compile java classes protobuf

问题

我在按照以下 YouTube 视频链接的5:09处进行Google的Protobuf教程时,在编译AddPerson.java类时遇到了问题。我一直在收到"addperson command not found"的错误消息。

这是我使用的命令。

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

我目前在运行Ubuntu Linux,并在终端中运行该命令。我觉得由于视频中的作者在Windows上使用命令提示符,所以可能需要稍微调整一下命令,使其适用于Linux系统。

英文:

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 found编译 Java 类的 Protocol Buffers 文件。
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.

答案1

得分: 1

在进一步的研究后,您使用 .: 来代替分号放在 .jar 文件名之前。

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

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

答案2

得分: 1

有两个明显的问题。

  1. 由于您正在运行Linux,类路径分隔符是:,而不是视频中显示的

  2. 您需要在类路径上包括包含您的代码的JAR文件或目录。我期望视频实际上是使用.字符来执行此操作的。请记住,在Windows和Linux上,.表示“当前目录”。

因此,您应该在Linux上像这样运行命令:

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

这假设您的AddPerson类没有package声明,并且AddPerson.class位于当前目录中。如果涉及包,情况会变得更加复杂。

请注意,在类路径上我将protobuf-java-3.5.1.jar的前面放置了.。在这种情况下可能并不重要,但一般来说顺序确实很重要。类加载器按特定顺序搜索类。

  • 首先,是系统类路径,其中包含标准库类。
  • 然后是Java安装的扩展目录。
  • 最后是应用程序类路径上的位置,按照它们在类路径上的指定顺序。

它找到的第一个具有正确(全限定)名称的类将“获胜”。因此,如果您的类路径是-cp protobuf-java-3.5.1.jar:.,并且在protobuf-java-3.5.1.jar中碰巧有一个AddPerson类,那么那个类将优先于您的类加载。


1 - 应用程序本身可以在运行时创建类加载器,进一步调整这个过程。但这是一个不同的话题。
2 - 适用于Java 8及更早版本。在Java 9中放弃了“扩展”机制的支持。
3 - 不用担心,这种情况不会发生。但重要的是要知道类路径的顺序可能很重要。

英文:

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

<sup>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.</sup>

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 order<sup>1</sup>.

  • first, the system classpath where the standard library classes live
  • then, the Java installation's Extensions directory<sup>2</sup>
  • 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 be<sup>3</sup> an AddPerson class in protobuf-java-3.5.1.jar, then that would be loaded in preference to your class.


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

答案3

得分: 1

以下为翻译好的内容:

在 Linux 环境中

编译

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

运行

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

在 Windows 环境中

编译

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

运行

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

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

huangapple
  • 本文由 发表于 2020年10月18日 08:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64408545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定