简体   繁体   English

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

[英]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.

相关问题 使用第三方jar在命令行中执行jar文件 - execute jar file in command line using third party jar 如何将第三方jar文件添加到我的android应用jar文件中? - How to add third-party jar files into my android application jar file? 与第三方命令行程序的提示符与Windows Command提示符的交互方式如何? - How is interacting with a prompt of a third party command line program different from a Windows Command prompt? 如何在liferay中部署第三方jar文件? - How to deploy third-party jar files in liferay? 在 PHPJAVA Bridge 中包含 .jar-File - Include .jar-File in PHPJAVA Bridge Maven 在构建我们自己的 custom.jar 时发布和使用第三方运行时 jar - Maven publish & consume the third-party runtime jars while building our own custom.jar 如何在java文件中包含jar文件并在命令提示符下编译 - How to include jar files with java file and compile in command prompt Maven-我可以在应用运行时添加第三方依赖项吗? - Maven - can i include third-party dependencies during app's runtime? 从命令行执行第三方jar,是否标记为忽略SSL问题? - Execute Third-Party Jar From Command Line, Flag To Ignore SSL Issues? 在 Windows 中使用命令提示符编译带有 Jar 依赖项的 Java 文件 - Compiling Java file with Jar dependencies using command prompt in windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM