简体   繁体   中英

Run a Java Class with Command-Line

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

And some more, but they don't work. Can somebody help me?

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

You can also check this answer for a similar solution.

Side note: You should follow the Java naming conventions . 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.j‌​ar" ExecutionEngine 

It doesn't matter where you put . ie the current directory eg 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.j‌​ar;." ExecutionEngine 

Note for Mac:

The separator used for this purpose in Mac is : instead of ; eg

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 without compiling eg

java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java

You can learn more about it from this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM