Run a .jar file using .sh file in unix

62,270

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
Share:
62,270
Harshita Sethi
Author by

Harshita Sethi

I'm coding since 2014 and fell in love with it ever since. My major strength areas are Java, JavaFX. Down the journey, I've also coded in HTML5, CSS, Javascript, Jquery, Angular JS, Restful services, WSDLs, Android app building and PL/SQL. I love programming and love to experiment with codes to figure out the depth and how far I can go with coding. I believe in improvising. No one's perfect but if someone decides not to give up until perfection they ought to learn a lot and gain wisdom throughout their journey, which is even better than being perfect the very first time. Through my journey, I have also improvised learned many ways of NOT to code and many on how to code. Loving the stack overflow journey. There's still a lot learn. My achievement I would like to say I'm proud to write code for a chatbot from scratch. The chatbot I built is applicable universally and can be used in any and every scenario. Training Data required offcourse!! I'm proud to tell that the chatbot I build (Accenture Genie) is a bit hit in my company. We are getting many clients who wants to try out the chatbot and use it as a support for their teams daily evolving issues. Support seems a lot simpler with the use of this bot.

Updated on April 29, 2020

Comments

  • Harshita Sethi
    Harshita Sethi about 4 years

    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.

  • Harshita Sethi
    Harshita Sethi about 9 years
    when I'm running the command java -jar DirectoryScanner.jar on unix, it is giving me the proper output. So I think the jar is fine, only thing is I need a command to be written in an sh file so that it can run that jar wherever it is present.
  • Tassos Bassoukos
    Tassos Bassoukos about 9 years
    Extract the MANIFEST.MF from your JAR (a JAR is structurally a ZIP file) and see what main class is specified there. Is it your own? Also, Inspect the contents, are your classfiles in there?
  • Harshita Sethi
    Harshita Sethi about 9 years
    Ya the jar is absolutely fine.
  • Rached Khalledi
    Rached Khalledi almost 3 years
    Hello, yeah this worked, but what could be the main purpose of this ? I mean why should access to the directory then launch the jar ? the java -jar /home/..../file.jar doesn't work