英文:
Run a Java Class with Command-Line
问题
以下是您要翻译的内容:
我有4个Java数据:
Common.java
Constants.java
KeywordsEditor.java
ExecutionEngine.java(这里有一个Main方法)
我已经成功在命令行中使用项目目录(C:\ProjectDemo\src\main\java\ValueInput)中的以下命令进行编译:
javac -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
我在相同的目录中得到了4个Data .class文件。现在我想用以下代码运行它们:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
但是我收到了错误消息:
Error: 找不到或加载 ExecutionEngine 主类
我尝试了一些相同的代码,比如:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs\*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs/*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
还有一些其他尝试,但它们都不起作用。有人可以帮我吗?
英文:
I have 4 Java-Data:
Common.java
Constants.java
KeywordsEditor.java
ExecutionEngine.java (There is here a Main-Method)
I have successful compiled in Command-Line with this Command from Project-Directory (C:\ProjectDemo\src\main\java\ValueInput)
javac -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
I got 4 Data .class in the same Directory. Now i want to run them with this code:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
But i got Error:
Error: ExecutionEngine main class could not be found or loaded
```
I've tried with some same code else:
```
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs\*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;libs/*;selenium-server-standalone-3.141.59.jar" ExecutionEngine
```
And some more, but they don't work. Can somebody help me?
</details>
# 答案1
**得分**: 1
# 更新
从您的评论中,我了解到在 `ExecutionEngine.java` 中提到了 `package ValueInput;`。因此,在编译时,您应该使用 `-d` 开关:
```shell
javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
```
选项 `-d .` 要求编译器将生成的类文件放置在当前目录中。现在,如果您在Mac/Unix中使用 `ls` 命令或在Windows中使用 `dir` 命令,您将看到一个名为 `ValueInput` 的目录已经创建,并且所有的 `.class` 文件已放置在此目录中。通过简单地使用 `javac` 命令来了解更多关于这些开关的信息。
为了执行 `ExecutionEngine.class`,您可以使用以下命令:
```shell
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ValueInput.ExecutionEngine
```
您还可以查看[此答案](https://stackoverflow.com/a/63319381/10819573)以获取类似的解决方案。
**附注:** 您应该遵循[Java命名约定](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html)。根据约定,包的名称应该类似于 `value.input`。
# 原始答案
问题的根本原因是只使用了 `-cp` 来添加JAR文件。您可能没有意识到您的 `ExecutionEngine.class` 不在JAR文件中;而是在当前目录中,由点号 (`.`) 表示,您忘记将其包含在类路径中。
因此,正确的命令应该是:
```shell
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
```
无论您将 `.` 即当前目录放在何处,以下命令也将适用:
```shell
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar;." ExecutionEngine
```
**Mac提示:**
在Mac中用于此目的的分隔符是 `:` 而不是 `;`,例如:
```shell
javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
```
**Java-11及以后版本提示:**
[Java-11允许启动单文件源代码程序][1]而无需编译,例如:
```shell
java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
```
您可以从[这篇文章](https://www.infoq.com/articles/single-file-execution-java11/)了解更多信息。
[1]: https://openjdk.java.net/jeps/330
<details>
<summary>英文:</summary>
# Update
From your comment, I learnt that you have `package ValueInput;` mentioned in `ExecutionEngine.java`. Therefore, you should use the switch `-d` when compiling:
javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" *.java
The option `-d .` asks the compiler to place the generated class files at the current directory. Now, if you use the command `ls` in Mac/Unix or `dir` in Windows, you will see a directory, `ValueInput` has been created and all the `.class` files have been placed inside this directory. Learn more about the switches by simply using the command `javac`
In order to execute `ExecutionEngine.class`, you can now use the following command:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ValueInput.ExecutionEngine
You can also check [this answer](https://stackoverflow.com/a/63319381/10819573) for a similar solution.
**Side note:** You should follow the [Java naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html). As per the convention, the name of the package should be something like `value.input`.
# Original answer
The root cause of the problem is using only jars with `-cp`. You missed realizing that your `ExecutionEngine.class` is not in the jars; rather it is at the current directory which is denoted by a dot (`.`) which you missed to include in the classpath.
Thus, the correct command will be:
java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar" ExecutionEngine
It doesn't matter where you put `.` i.e. the current directory e.g. the following will also work for you:
java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.jar;." ExecutionEngine
**Note for Mac:**
The separator used for this purpose in Mac is `:` instead of `;` e.g.
javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
**Note for Java-11 onwards:**
[Java-11 allows launching Single-File Source-Code programs][1] without compiling e.g.
java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
You can learn more about it from [this article](https://www.infoq.com/articles/single-file-execution-java11/).
[1]: https://openjdk.java.net/jeps/330
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论