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.
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
Running a jar with -cp
requires the classpath to list the jar, for instance :
java -cp "Test.jar;lib/*" my.pretty.MainClass
NB : Test.jar
can be replaced by *
to match all jars, but *.jar
won't work.
Alternatively, you can run the jar with -jar
. This method requires the classpath to be defined in the jar's manifest and silently ignores other classpath declarations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.