6 Replies Latest reply on Jan 12, 2016 11:56 PM by Bennett

    Run Java application using script

    Bennett

      The eventual goal is to run a Java application on startup. First I am trying to run it by calling a script from the command line.

       

      I can run a Java application from the command line by following command, which comes from this guide https://software.intel.com/en-us/node/596288

      where "sample.jar" is the name of compiled classes JAR, and "Sample" is the name of the main Java class.

       

      java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:sample.jar Sample
      

       

      However, I have been unable to use this in a script, so that I can run it on startup. If I create a script with the same command, as shown below...

      #!/bin/bash
      
      java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:sample.jar Sample
      


      I get the following error:

      Error: Could not find or load main class ?cp
      

       

      In case it wasn't obvious, I don't know much about shell scripts. Any help would be most appreciated. Thanks!

        • 1. Re: Run Java application using script
          ttc7152

          looks like you probably need the full path for your sample.jar ... something like:

           

          java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:/home/root/path/to/your/jar/sample.jar Sample


          in terms of your question on running on startup, i had luck doing it this way (i'm sure there is a better way):


          1- setup a service, this is part of the built in framework for running processes at startup with linux/yocto - there should be some info in this thread How do I run a script on startup on Edison?

          2- for some reason i didn't have any luck getting a script to run from my service, it may have been a permissioning issue, but i had luck with a simple executable c file, which then called my script.  you will probably have to chmod whatever you are calling from your service so they are executable.

          3- finally, within the script, you can make your call out to run your java app

          1 of 1 people found this helpful
          • 2. Re: Run Java application using script
            CMata_Intel

            Hi Bennett,

             

            Which image are you using on your board? Run configure_edison --version, I would like to test this using your environment.

            Without using a script to run at startup, what is the output you are getting? Are you having the same output?

             

            Regards,

            Charlie

            • 3. Re: Run Java application using script
              Bennett

              configure_edison --version gives me:

              159.devkit

               

              If I enter the following in the command line, then the program just starts running, no problems.

              java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:sample.jar Sample  
              
              
              • 4. Re: Run Java application using script
                ttc7152

                right, the error about not finding the class is being thrown by the JVM, so nothing really to do with the linux image, version, etc. 

                 

                you just need to ensure that the java command being executed by your script has the correct classpath to find your jar. 

                 

                is your script being executed in the same directory as where you are running your "command line" example?

                 

                you might also try:

                java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:./sample.jar Sample

                1 of 1 people found this helpful
                • 5. Re: Run Java application using script
                  Bennett

                  Wow, this was a tricky one to track down. And I only randomly stumbled on the solution. It seems that when I copied and pasted the code from the Intel example, the hyphen before the "cp" was the wrong kind of hyphen. At some point, I must have rewritten the -cp command in my script, which inserted the correct hyphen. It all makes sense given the error I was receiving, because it wasn't treating "cp" as an option, but was trying to find a class called "cp".

                  Below are the two different commands.

                   

                  From the web tutorial:

                  java -Djava.library.path=/usr/lib/java –cp .:/usr/lib/java/*:sample.jar Sample
                  

                   

                  Correct:

                  java -Djava.library.path=/usr/lib/java -cp .:/usr/lib/java/*:sample.jar Sample
                  

                  See the difference in the "-cp"?

                   

                  I appreciate everyone's input. Hopefully all these answers will help somebody else down the road.

                  Every time something like this happens, I tell myself I'm going to quit the "copy/paste" business, and then 5 minutes later I'm back at it...

                  • 6. Re: Run Java application using script
                    Bennett

                    ttc7152 Thanks for the advice. That thread was indeed helpful! From there, I was directed to this post, which worked right away (finally!). Automatic Scripting at Boot-Up | Musings from Stephanie

                    The tutorial is very complete, so I won't bother repeating anything here. One additional piece of information that might be useful is how to remove a startup script after you've added it. I found that here (which also includes how to add a startup script): Making scripts run at boot time with Debian

                     

                    Thanks again for everyone's help!

                    Greg