Run a Java Class with Command-Line 使用命令行运行Java类

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

Run a Java Class with Command-Line

问题

以下是您要翻译的内容:

我有4个Java数据:

  1. Common.java
  2. Constants.java
  3. KeywordsEditor.java
  4. ExecutionEngine.java(这里有一个Main方法)

我已经成功在命令行中使用项目目录(C:\ProjectDemo\src\main\java\ValueInput)中的以下命令进行编译:

  1. 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.j‌​ar" *.java

我在相同的目录中得到了4个Data .class文件。现在我想用以下代码运行它们:

  1. 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.j‌​ar" ExecutionEngine

但是我收到了错误消息:

  1. Error: 找不到或加载 ExecutionEngine 主类

我尝试了一些相同的代码,比如:

  1. 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.j‌​ar" ExecutionEngine
  2. 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.j‌​ar" ExecutionEngine

还有一些其他尝试,但它们都不起作用。有人可以帮我吗?

英文:

I have 4 Java-Data:

  1. Common.java
  2. Constants.java
  3. KeywordsEditor.java
  4. 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)

  1. 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.j‌​ar" *.java

I got 4 Data .class in the same Directory. Now i want to run them with this code:

  1. 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.j‌​ar" ExecutionEngine

But i got Error:

  1. Error: ExecutionEngine main class could not be found or loaded
  2. ```
  3. I've tried with some same code else:
  4. ```
  5. 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.j‌​ar" ExecutionEngine
  6. 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.j‌​ar" ExecutionEngine
  7. ```
  8. And some more, but they don't work. Can somebody help me?
  9. </details>
  10. # 答案1
  11. **得分**: 1
  12. # 更新
  13. 从您的评论中,我了解到在 `ExecutionEngine.java` 中提到了 `package ValueInput;`。因此,在编译时,您应该使用 `-d` 开关:
  14. ```shell
  15. 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
  16. ```
  17. 选项 `-d .` 要求编译器将生成的类文件放置在当前目录中。现在,如果您在Mac/Unix中使用 `ls` 命令或在Windows中使用 `dir` 命令,您将看到一个名为 `ValueInput` 的目录已经创建,并且所有的 `.class` 文件已放置在此目录中。通过简单地使用 `javac` 命令来了解更多关于这些开关的信息。
  18. 为了执行 `ExecutionEngine.class`,您可以使用以下命令:
  19. ```shell
  20. 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
  21. ```
  22. 您还可以查看[此答案](https://stackoverflow.com/a/63319381/10819573)以获取类似的解决方案。
  23. **附注:** 您应该遵循[Java命名约定](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html)。根据约定,包的名称应该类似于 `value.input`。
  24. # 原始答案
  25. 问题的根本原因是只使用了 `-cp` 来添加JAR文件。您可能没有意识到您的 `ExecutionEngine.class` 不在JAR文件中;而是在当前目录中,由点号 (`.`) 表示,您忘记将其包含在类路径中。
  26. 因此,正确的命令应该是:
  27. ```shell
  28. 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
  29. ```
  30. 无论您将 `.` 即当前目录放在何处,以下命令也将适用:
  31. ```shell
  32. 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
  33. ```
  34. **Mac提示:**
  35. 在Mac中用于此目的的分隔符是 `:` 而不是 `;`,例如:
  36. ```shell
  37. javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
  38. java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
  39. ```
  40. **Java-11及以后版本提示:**
  41. [Java-11允许启动单文件源代码程序][1]而无需编译,例如:
  42. ```shell
  43. java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
  44. ```
  45. 您可以从[这篇文章](https://www.infoq.com/articles/single-file-execution-java11/)了解更多信息。
  46. [1]: https://openjdk.java.net/jeps/330
  47. <details>
  48. <summary>英文:</summary>
  49. # Update
  50. From your comment, I learnt that you have `package ValueInput;` mentioned in `ExecutionEngine.java`. Therefore, you should use the switch `-d` when compiling:
  51. javac -d . -cp &quot;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar&quot; *.java
  52. 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`
  53. In order to execute `ExecutionEngine.class`, you can now use the following command:
  54. java -cp &quot;.;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar&quot; ValueInput.ExecutionEngine
  55. You can also check [this answer](https://stackoverflow.com/a/63319381/10819573) for a similar solution.
  56. **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`.
  57. # Original answer
  58. 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.
  59. Thus, the correct command will be:
  60. java -cp &quot;.;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar&quot; ExecutionEngine
  61. It doesn&#39;t matter where you put `.` i.e. the current directory e.g. the following will also work for you:
  62. java -cp &quot;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar;.&quot; ExecutionEngine
  63. **Note for Mac:**
  64. The separator used for this purpose in Mac is `:` instead of `;` e.g.
  65. javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
  66. java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo
  67. **Note for Java-11 onwards:**
  68. [Java-11 allows launching Single-File Source-Code programs][1] without compiling e.g.
  69. java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
  70. You can learn more about it from [this article](https://www.infoq.com/articles/single-file-execution-java11/).
  71. [1]: https://openjdk.java.net/jeps/330
  72. </details>

huangapple
  • 本文由 发表于 2020年8月12日 23:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63379771.html
匿名

发表评论

匿名网友

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

确定