简体   繁体   English

从 java 中的批处理文件运行 jar

[英]running jar from batch file in java

I am having problem to run the main class from the jar.我在从 jar 运行主 class 时遇到问题。

Ant script has produced following folders: Ant 脚本产生了以下文件夹:

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.我只想 go 运行文件夹并双击 .bat 文件并运行应用程序。

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 .您似乎正在将多个 JAR 文件传递给java 应用程序启动器 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.您需要传入一个单一的 jar 文件(在本例中为 MyProject.jar),该文件用作入口点。 All related JARs should be specified in ClassPath entries of the manifest MANIFEST.MF , of the main jar.所有相关的 JARs 应在主 jar 的清单 MANIFEST.MF的 ClassPath 条目中指定。 The manifest should also specify the Main class - the one having the main() method.清单还应指定 Main class - 具有 main() 方法的那个。

If you want to avoid the above, and specify the complete classpath on the command line, use the -cp or -classpath flag.如果您想避免上述情况,并在命令行上指定完整的类路径,请使用-cp-classpath标志。 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:在 Windows 上,如果需要,将类路径条目用引号括起来:

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.根据现在报告的新错误消息, Microsoft SQL 服务器 JDBC 驱动程序似乎不存在于类路径中。 This would require downloading and placing the necessary JARs (in the lib directory).这需要下载并放置必要的 JARs(在 lib 目录中)。 If the driver is present elsewhere, then the above command used to launch the application should be updated with the location of the JAR.如果驱动程序存在于其他地方,则上述用于启动应用程序的命令应使用 JAR 的位置进行更新。

Use -cp (or -classpath) instead of -jar:使用 -cp(或 -classpath)而不是 -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. -jar 选项用于运行 .jar 文件,这要求 .jar 文件必须包含一个清单,说明要执行哪个 class。 But here you don't want that, because you are already supplying the class to run (com.my.test.MainTest).但在这里你不希望这样,因为你已经提供了 class 来运行 (com.my.test.MainTest)。

Update:更新:

As @Rob mentions, the way to use wildcards in the classpath is just '*', not '*.jar', so you really want:正如@Rob 提到的,在类路径中使用通配符的方法只是'*',而不是'*.jar',所以你真的想要:

java -cp ../dist/MyProject.jar;../lib/* com.my.test.MainTest
  1. Use -cp to specify the classpath使用-cp指定类路径
  2. Remove the .jar extension for the wildcard删除通配符的.jar扩展名

Your resulting command would be:您生成的命令将是:

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

Related question with enlightening answers.具有启发性答案的相关问题

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

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