简体   繁体   English

无法通过 windows 命令行运行 Java 主 class

[英]Unable to run Java main class though windows commandline

I have an external jar and I have written 2 classes named 'TestConnector.java' and 'APIExampleUsageTestData.java' and I have placed these 2 files under default package.我有一个外部 jar,我编写了 2 个名为“TestConnector.java”和“APIExampleUsageTestData.java”的类,并将这两个文件放在默认的 package 下。

I have placed all the 3 artifacts -> externalConnector.jar, TestConnector.java and APIExampleUsageTestData.java into a folder named 'temp'.我已将所有 3 个工件 -> externalConnector.jar、TestConnector.java 和 APIExampleUsageTestData.Z93F725A07423FE1C889F448B33D21F46 放入文件夹中

I have opened commandline in the temp folder context and compiled the class files with below command and it executed without any errors:我已经在临时文件夹上下文中打开了命令行,并使用以下命令编译了 class 文件,它执行时没有任何错误:

javac -cp "externalConnector.jar" *.java

I can see that there are 2 class files present in the tem folder.我可以看到 tem 文件夹中有 2 个 class 文件。

There is a main method in the APIExampleUsageTestData.class and I'm trying to call that class with the below command, but, is failing APIExampleUsageTestData.class 中有一个主要方法,我正在尝试使用以下命令调用该 class,但是失败了

When running the below command in 'C:\Users\AppData\Local\Temp\0.15429262750877082' folder context在“C:\Users\AppData\Local\Temp\0.15429262750877082”文件夹上下文中运行以下命令时

java -cp "externalConnector.jar" APIExampleUsageTestData

I'm getting error Error: Could not find or load main class APIExampleUsageTestData我收到错误Error: Could not find or load main class APIExampleUsageTestData

When I trying to run the command, without any class path entry to check what error it is throwing:当我尝试运行命令时,没有任何 class 路径条目来检查它抛出的错误:

java APIExampleUsageTestData

It throws error: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException它抛出错误: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException

It is a Windows Operating System.它是一个 Windows 操作系统。 Could you please let me know how to run the program successfully?你能告诉我如何成功运行程序吗?

You need both the current directory and the jar file to be on the classpath.您需要将当前目录jar 文件放在类路径上。 So you want (Linux / Mac):所以你想要(Linux / Mac):

java -cp .:externalConnector.jar APIExampleUsageTestData

Or (Windows)或 (Windows)

java -cp .;externalConnector.jar APIExampleUsageTestData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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