Java 在 unix 中使用 .sh 文件运行 .jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29847709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:41:24  来源:igfitidea点击:

Run a .jar file using .sh file in unix

javabashshellunixjar

提问by Harshita Sethi

I have a jar file DirectoryScanner.jarcreated in windows 7. I want to execute this jar on a unix server. I ran the following command in putty and the jar run absolutely fine as expected:

DirectoryScanner.jar在 Windows 7 中创建了一个 jar 文件。我想在 unix 服务器上执行这个 jar。我在 putty 中运行了以下命令,jar 运行完全符合预期:

java -jar DirectoryScanner.jar

Now I want to create a .sh file on the unix server which on executing can run this jar. I created a file Report.sh in which I wrote following code to execute this jar:

现在我想在 unix 服务器上创建一个 .sh 文件,该文件在执行时可以运行这个 jar。我创建了一个文件 Report.sh,我在其中编写了以下代码来执行这个 jar:

java -cp /home/applvis/Java/UAT/lib/DirectoryScanner.jar com.acc.directory.scanner.SDScanner

But when I execute this command in putty, it shows the following error:

但是当我在 putty 中执行此命令时,它显示以下错误:

[applvis@bg6lnxebs1 UAT]$ . ./ReportGen.sh
Exception in thread "Main Thread" java.lang.NoClassDefFoundError: com/acc/directory/scanner/SDScanner
Caused by: java.lang.ClassNotFoundException: com.acc.directory.scanner.SDScanner
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.acc.directory.scanner.SDScanner.  Program will exit.

Can anyone tell me what exactly I'm doing wrong, or suggest some alternate command.

谁能告诉我我到底做错了什么,或者建议一些替代命令。

Both my jar and sh file are in different directories. Even if they are in same directory, I get this error.

我的 jar 和 sh 文件都在不同的目录中。即使它们在同一目录中,我也会收到此错误。

Ps. I have many jar files to be executed one after the other. So rather than again and again writing the command to run each jar separartely on unix, I want to create an sh file which will contain the code to run all the jars one after the other. And it will be easier for me to just run the sh file. Hence I need the code to be written in sh file which can run my jars.

附言。我有许多 jar 文件要一个接一个地执行。因此,与其一遍又一遍地编写命令以在 unix 上单独运行每个 jar,我想创建一个 sh 文件,该文件将包含一个接一个运行所有 jar 的代码。我只运行 sh 文件会更容易。因此,我需要将代码编写在可以运行我的 jars 的 sh 文件中。

采纳答案by thonnor

In your shell script change into the directory containing the jar files. Possibly not the best practice but I use this all the time to emulate a 'working directory' where scripts are started from. This way my shell script can be installed in a scriptsdirectory and my Java can be installed in a libdirectory.

在您的 shell 脚本中,切换到包含 jar 文件的目录。可能不是最佳实践,但我一直使用它来模拟启动脚本的“工作目录”。这样我的 shell 脚本就可以安装在一个scripts目录中,而我的 Java 可以安装在一个lib目录中。

Assuming your environment is the same when you execute the script as when you call the java from the command line.

假设您执行脚本时的环境与从命令行调用 java 时的环境相同。

#!/bin/sh

cd /home/applvis/Java/UAT/lib
java -jar DirectoryScanner.jar

回答by Tassos Bassoukos

Depending on the tool you used to generate the JAR, it may have replaced your main class with something else that f.e. sets up the classpath and then calls your class. This happens a lot when generating fat JARs with all the dependencies. Do check the contents of the JAR, as it may contain other JARs in turn.

根据您用于生成 JAR 的工具,它可能已将您的主类替换为其他东西,即 fe 设置类路径然后调用您的类。在生成具有所有依赖项的胖 JAR 时,这种情况经常发生。请检查 JAR 的内容,因为它可能依次包含其他 JAR。