[英]java run command from cmd
我想用cmd命令運行我的java類。 因此,我有下一個結構:
..
lib/org.json.jar
src/main/java/Parser.java
現在,在構建Maven之后,我在目標中有了一些文件夾:
classes/parser.class
lib/org.json.org
因此,現在我嘗試執行下一條命令:
java - cp package.report
但是我有下一個錯誤:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
我也嘗試執行以下命令: java -cp "org.json.jar" package.report
並得到以下錯誤:
Error: Could not find or load main class package.report
我究竟做錯了什么?
看來您需要做兩件事:
獲取需要正確添加到類路徑中的JAR文件,該文件需要對實際文件的完整引用:-cp“ the / full / path / to / the / lib / directory / andTheCompleteJarFilename.jar”
在您的類路徑上獲取編譯的類,該類路徑只需要您的/ classes根目錄的路徑:-cp“ the / full / path / to / your / classes / directory”
要在類路徑規范中將這兩者結合在一起,只需使用分號作為分隔符:-cp“ the / jar / file / path / andFullName.jar; the / path / to / the / class / directory”
最后,將其添加到java run命令中,它將類似於:java -cp“ jar / path / name.jar; full / path / to / class / dir” full.package.structure.path.AndTheClassName
這應該使您啟動並運行
如果要使用jara -jar my.jar從命令行運行Main方法,則需要使用maven構建一個jar文件。
您將需要如下所示的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>...</artifactId>
<packaging>jar</packaging>
<properties>
<encoding>UTF-8</encoding>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com....commandLineClssTestRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后像這樣的assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>META-INF/spring.handlers</exclude>
<exclude>META-INF/spring.schemas</exclude>
</excludes>
</unpackOptions>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${project.basedir}/src/main/resources/META-INF/spring.handlers</source>
<outputDirectory>META-INF</outputDirectory>
</file>
<file>
<source>${project.basedir}/src/main/resources/META-INF/spring.schemas</source>
<outputDirectory>META-INF</outputDirectory>
</file>
<file>
<source>${project.basedir}/src/main/resources/logback.xml</source>
</file>
</files>
</assembly>
然后您可以使用
java -jar my.jar
這真的取決於您追求什么? 如果您想要易於運行的uberjar,則可以使用Apache Maven Shade插件。 這將允許您創建一個捆綁了所有內容的單個jar文件,准備運行。
另一方面,如果只需要類路徑才能在代碼上運行某些臨時測試,則可以使用Maven依賴插件獲取正確的類路徑字符串,該字符串使用正確的路徑分隔符(取決於您的操作系統)列出所有依賴項。 只需輸入maven dependency:build-classpath
。 然后,您可以采用此路徑並在對java
的調用中使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.