简体   繁体   English

使用cmd运行* .jar文件并与其他* .jar文件具有某些依赖性

[英]Running *.jar file with some dependencies to other *.jar files using cmd

I have program in Java in path: C:\\...\\MyProgram . 我在路径为C:\\...\\MyProgram Java中有程序。 This program have some dependencies to others *.jar files. 该程序与其他* .jar文件具有某些依赖性。 I would run it using cmd. 我会使用cmd运行它。 So what I do: 所以我要做的是:

in cmd I write cd C:\\...\\MyProgram\\bin and then java -cp C:\\...\\MyProgram\\*;. main.Main 在cmd中,我写cd C:\\...\\MyProgram\\bin ,然后写java -cp C:\\...\\MyProgram\\*;. main.Main java -cp C:\\...\\MyProgram\\*;. main.Main . java -cp C:\\...\\MyProgram\\*;. main.Main It is working. 这是工作。 But now I exported MyProgram to jar file. 但是现在我将MyProgram导出到jar文件。 Could you tell me how can I run this now? 你能告诉我我现在该怎么办吗? So I have file MyProgram.jar with these same dependencies. 因此,我具有相同的依赖关系文件MyProgram.jar。 How run it by using cmd? 如何使用cmd运行它?

Folders and archive files 文件夹和档案文件

When classes are stored in a directory (folder), like /java/MyClasses/utility/myapp, then the class path entry points to the directory that contains the first element of the package name. 如果将类存储在目录(文件夹)中,例如/ java / MyClasses / utility / myapp,则类路径条目将指向包含包名称第一个元素的目录。 (in this case, /java/MyClasses, since the package name is utility.myapp.) (在这种情况下,为/ java / MyClasses,因为程序包名称为Utility.myapp。)

But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. 但是,当类存储在存档文件(.zip或.jar文件)中时,类路径条目是.zip或.jar文件的路径,包括该路径。 For example, to use a class library that is in a .jar file, the command would look something like this: 例如,要使用.jar文件中的类库,该命令将如下所示:

% java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool %java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool

Found in http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html中找到

You need to add a Class-Path entry to the manifest file (META-INF/manifest.mf) inside the jar: 您需要在jar内的清单文件(META-INF / manifest.mf)中添加Class-Path条目:

Class-Path: /C:/.../MyProgram/ . 类路径:/C:/.../MyProgram/。

This assumes that there are dependent classes under C:/.../MyProgram/, not jar files. 假定C:/.../ MyProgram /下有依赖类,而不是jar文件。

You should also add an entry for the Main-class: 您还应该为Main-class添加一个条目:

Main-Class: main.Main 主类:main.Main

Then just execute your jar as 然后只需执行您的罐子

java -jar MyProgram.jar java -jar MyProgram.jar

Set the path of JAR file in your classpath and then execute the other JAR file. classpath路径中设置JAR文件的classpath ,然后执行其他JAR文件。

To add JAR using eclipse. 使用Eclipse添加JAR。

  1. Right click on project -> properties 右键单击项目->属性

  2. Java Build Path -> Click add external JARs. Java Build Path ->单击添加外部JAR。

  3. This will add JAR to your classpath. 这会将JAR添加到您的类路径中。

在此处输入图片说明

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

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