在UNIX中使用.sh文件运行.jar文件 [英] Run a .jar file using .sh file in unix

查看:194
本文介绍了在UNIX中使用.sh文件运行.jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7中创建了一个jar文件DirectoryScanner.jar.我想在unix服务器上执行此jar. 我在腻子中运行了以下命令,然后jar可以按预期运行完全正常:

I have a jar file DirectoryScanner.jar created 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:

java -jar DirectoryScanner.jar

现在,我想在unix服务器上创建一个.sh文件,该文件在执行时可以运行此jar.我创建了一个Report.sh文件,在其中编写了以下代码来执行此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:

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:

[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$1.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.

我的jar和sh文件都位于不同的目录中.即使它们在同一目录中,也会出现此错误.

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

Ps.我有很多jar文件要一个接一个地执行.因此,我不想创建一个在文件中单独运行每个jar的命令,而不是一遍又一遍地编写命令,而是要创建一个sh文件,该文件将包含运行所有jar的代码.对我来说,只运行sh文件会更容易.因此,我需要将代码编写为可以运行我的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.

推荐答案

在您的Shell脚本中,切换到包含jar文件的目录.可能不是最佳做法,但我一直使用它来模仿脚本从其开始的工作目录".这样,我的shell脚本可以安装在scripts目录中,而Java可以安装在lib目录中.

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 scripts directory and my Java can be installed in a lib directory.

假定执行脚本时的环境与从命令行调用Java时的环境相同.

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

#!/bin/sh

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

这篇关于在UNIX中使用.sh文件运行.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆