简体   繁体   English

在jar文件中使用java -cp时出错

[英]Error when using java -cp with jar file

I am trying to run a jar file using java -cp but it keeps giving me error that could not find or load main class. 我正在尝试使用java -cp运行一个jar文件,但它一直给我找不到或加载主类的错误。

Here is my bat file 这是我的蝙蝠文件

:: install.bat
:: run the installer jar as a windows bat file
::
@echo off

:: setup some properties
set CLASSES=.
set JAVA_OPTS=


:: build a basic classpath
set CP="%CLASSES%"

:: locate the java executable
set JAVA_EXE="%JAVA_HOME%\bin\java.exe"
IF EXIST "%JAVA_EXE%" GOTO start
set JAVA_EXE=java.exe

:start
:: show off java version info
echo -------------------------------------------------
"%JAVA_EXE%" -version
REM echo     JAVA_OPTS: %JAVA_OPTS%
REM echo     CLASSPATH: %CP%
echo -------------------------------------------------

:: start the program
%JAVA_EXE% %JAVA_OPTS% -cp %CP% -    Djava.util.logging.config.file="./enr_logging.properties" com.company.product.package.MainClass

Assuming that my fully qualified class name is com.company.product.package.MainClass and the jar is in the same folder as the .bat file 假设我的完全合格的类名称为com.company.product.package.MainClass,并且jar与.bat文件位于同一文件夹中

Running a jar with -cp requires the classpath to list the jar, for instance : 使用-cp运行jar需要类路径列出jar,例如:

java -cp "Test.jar;lib/*" my.pretty.MainClass

NB : Test.jar can be replaced by * to match all jars, but *.jar won't work. 注意: Test.jar可以用*替换以匹配所有jar,但是*.jar将不起作用。

Alternatively, you can run the jar with -jar . 或者,您可以使用-jar运行jar。 This method requires the classpath to be defined in the jar's manifest and silently ignores other classpath declarations. 此方法要求在jar的清单中定义类路径,并且静默忽略其他类路径声明。

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

相关问题 Java 一起使用“-cp”和“-jar” - Java using “-cp” and “-jar” together scala jar文件由java执行 - cp与jar - scala jar file executed by java - cp vs. jar 使用 -jar 选项运行 java 命令时,您可以使用 -cp 提供额外的库吗? - When running a java command with the -jar option, can you supply additional libraries using -cp? 为什么 Java -cp 选项在使用 spring-boot-maven-plugin 时 -jar 不起作用? - Why is the Java -cp option not working when -jar does while using the spring-boot-maven-plugin? Java:使用-cp选项运行的jar文件中的启动画面? - Java: splash screen in a jar file run with the -cp option? Java与jar一起运行时不遵循-cp选项的类路径规范 - Java not honoring the classpath specification with -cp option when running with a jar 使用Java中的Cp1250创建文件 - Create a file using Cp1250 in Java Google Dataflow作业在Eclipse上运行时工作正常,但是当我使用mvn进行编译并使用java -cp命令运行文件时出现错误 - Google Dataflow job working fine when running on eclipse but gives an error when i compile it using mvn and run the file using java -cp command java -cp“lib / *”与java -cp“lib / * .jar” - java -cp “lib/*” versus java -cp “lib/*.jar” SpringBoot:我可以在使用Java -cp提供的JAR中从可运行的JAR中@Autowire Bean吗? - SpringBoot: Can I @Autowire Bean in runnable JAR from JAR provided using java -cp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM