Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a .jar file using .sh file in unix

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

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.

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

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.

like image 231
Harshita Sethi Avatar asked Apr 24 '15 12:04

Harshita Sethi


People also ask

How do I run a .jar file?

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]

How do I read a jar file in Linux?

JAR files are packaged in the ZIP file format. The unzip command is a commonly used utility for working with ZIP files from the Linux command-line. Thanks to the unzip command, we can view the content of a JAR file without the JDK.


1 Answers

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.

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
like image 181
thonnor Avatar answered Oct 08 '22 18:10

thonnor