简体   繁体   中英

running jar from batch file in java

I am having problem to run the main class from the jar.

Ant script has produced following folders:

MyProject(Somewhere in C:)
 |
 |
 |____configuration(this contains properties/XML file)
 |
 |____dist(contains MyProject.jar)
 |
 |____lib(contains all other jars)
 |
 |____run(contains batch file to run MyProject.jar)

Inside run folder I have a batch file which reads like this:

java -jar ../dist/MyProject.jar;../lib/*.jar com.my.test.MainTest

Can somebody guide me. I just want to go to run folder and double click the.bat file and run the application.

I am getting

Exception in thread "main" java.lang.NoClassDefFoundError: MyProject/jar

Update

The new Error is:

The java class is not found:  com.microsoft.sqlserver.jdbc.SQLServerException

Thanks...

It seems like you are passing in multiple JAR files to thejava application launcher . That's not how it works.

You'll need to pass in a singular jar file (MyProject.jar in this case), which serves as the entry point. All related JARs should be specified in ClassPath entries of the manifest MANIFEST.MF , of the main jar. The manifest should also specify the Main class - the one having the main() method.

If you want to avoid the above, and specify the complete classpath on the command line, use the -cp or -classpath flag. However, you'll need to specify wildcards on the classpath, in a manner different from the one listed in the question. The following might work; on Windows, wrap the classpath entries in quotes if needed:

REM notice the quotes in the cp argument. Those are to be omitted in *nix
java -cp "../dist/MyProject.jar;../lib/*" com.my.test.MainTest

Update

Based on the new error message now reported, it appears that the Microsoft SQL Server JDBC Driver is not present in the classpath. This would require downloading and placing the necessary JARs (in the lib directory). If the driver is present elsewhere, then the above command used to launch the application should be updated with the location of the JAR.

Use -cp (or -classpath) instead of -jar:

java -cp ../dist/MyProject.jar;../lib/*.jar com.my.test.MainTest

The -jar option is used for running a.jar file, which requires that the.jar file must include a manifest saying which class to execute. But here you don't want that, because you are already supplying the class to run (com.my.test.MainTest).

Update:

As @Rob mentions, the way to use wildcards in the classpath is just '*', not '*.jar', so you really want:

java -cp ../dist/MyProject.jar;../lib/* com.my.test.MainTest
  1. Use -cp to specify the classpath
  2. Remove the .jar extension for the wildcard

Your resulting command would be:

    java -cp ..\dist\MyProject.jar;..\lib\* com.my.test.MainTest

Related question with enlightening answers.

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.

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