如何在 Windows 10 的命令提示符中编译和运行时包含第三方的 jar 文件?

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

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

问题

我正在尝试理解如何在 Windows 10 中使用命令行将第三方 JAR 文件包含到 Java 项目中。

具体来说,我尝试将文件 json-20200518.jar 包含到我的“项目”中,以便我可以在项目中使用 Java 对象 JSONObject。

我的 Java 文件:

package com.mypackage.example;

import org.json.JSONObject;

class Example {
    public static void main(String[] args) {
        // ... 程序逻辑
    }
}

我的 Java 文件位置(Examp.java):

./com/mypackage/example

JAR 文件位置:

./jars

使用 Windows 10 的命令提示符进行编译:

javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"

编译成功。

运行:

java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp

我收到一个报告:

Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus

第二次尝试:

java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"

但是我收到相同的错误信息。
我在哪里做错了?可能是目录结构出错了吗?我不太明白,编译是成功的,但运行却不起作用。

英文:

I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.

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.

My java file:

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):
> ./com/mypackage/example

location of jar file:
> ./jars

using cmd win10 I compile:
> 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

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

Second attempt:
> 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?<br> Is it the wrong structure?<br> I don't get it, the compilation is successful but the run does not work.

答案1

得分: 1

编译后的 Examp.class 文件不包含在 json-20200518.jar 中,因此您需要将包含该文件的目录添加到命令行。假设它是当前目录(.):

java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp
英文:

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. Assuming it's the current directory (.):

java -cp &quot;C:\Users\pfort\Desktop\java\jars\json-20200518.jar;.&quot; com.mypackage.example.Examp

huangapple
  • 本文由 发表于 2020年9月28日 18:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64100306.html
匿名

发表评论

匿名网友

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

确定