[英]How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?
I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.我正在尝试仅使用 Windows 10 中的命令行来了解在 Java 项目中包含第三方 jar 文件。
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.具体来说,我尝试在我的“项目”中包含文件 json-20200518.jar,以便我可以在项目中使用 java 对象 JSONObject。
My java file:我的java文件:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):我的 java 文件的位置(Examp.java):
./com/mypackage/example ./com/mypackage/示例
location of jar file: jar 文件的位置:
./jars ./罐子
using cmd win10 I compile:使用 cmd win10 我编译:
javac -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" "C:\\Users\\pfort\\Desktop\\java\\com\\mypackage\\example\\Examp.java" javac -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" "C:\\Users\\pfort\\Desktop\\java\\com\\mypackage\\example\\Examp.java"
compilation is successful.编译成功。
Run:跑:
java -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" com.mypackage.example.Examp java -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" com.mypackage.example.Examp
I get a report:我收到一份报告:
Error: Could not find or load main class com.mypackage.example.Pokus Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus错误:无法找到或加载主类 com.mypackage.example.Pokus 导致:java.lang.ClassNotFoundException:com.mypackage.example.Pokus
Second attempt:第二次尝试:
java -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" "C:\\Users\\pfort\\Desktop\\java\\com\\mypackage\\example\\Pokus" java -cp "C:\\Users\\pfort\\Desktop\\java\\jars\\json-20200518.jar" "C:\\Users\\pfort\\Desktop\\java\\com\\mypackage\\example\\Pokus"
But the same error message comes back to me.但是同样的错误信息又回到了我身上。 Where am I going wrong?我哪里错了?
Is it the wrong structure?是不是结构不对?
I don't get it, the compilation is successful but the run does not work.没看懂,编译成功但是运行不行。
The compiled Examp.class
file isn't part of json-20200518.jar
, so you'll need to add the directory containing it to the command line.编译后的Examp.class
文件不是json-20200518.jar
一部分,因此您需要将包含它的目录添加到命令行。 Assuming it's the current directory ( .
):假设它是当前目录( .
):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.