0

I have been trying to execute jar files. I first learnt how to execute them through cmd and I did fairly well. Now, I am currently trying to run the jar file from Java code that I have been writing. The jar is located in my machine and I run the jar through jar code as shown below:

Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec(new String[] { "cmd", "/c", "start C:\\jartest\\JavaApplication.jar" });

This works really good on my machine. The output of JavaApplication.jar is that it creates a folder and the output is achieved.

The problem occurs, when I want to run a Jar file located at Raspberry PI from my machine through Servlet. The code is as shown below:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    QLM qlm = new QLM();
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("Response of Servlet Content Started \n");
        String message= request.getParameter("msg");
        out.println(message);
        if((qlm.get_Envelop(message))!= null){
            out.println("DATA IS VALID");
        } else out.println("WRONG MESSAGE!!");
        out.println("Response of Servlet Content end \n");

        **Runtime r = Runtime.getRuntime();
        out.println("Am I here??");
        r.exec(new String[] {"cmd","/c", "start /home/pi/JARTest/JavaApplication24.jar", "file1" });**
        out.println("Did it work??");

    } finally {            
        out.close();
    }
}

It is the same jar file that was on my Machine. I put/store JavaApplication.jar on PI through WinSCP. The path where the jar located on Raspberry PI is: /home/pi/JARTest. I pass the command line argument as "file1" to the Jar file. The other operations of servlet works fine as well, except from running the Jar file.

P.S- The result of the jar (JavaApplication.jar) is same. It works on my machine (Windows), but it simply doesn't want to accept the same jar file and the same command Runtime r = Runtime.getRuntime(); on Raspberry PI.

Please help me with this. Any input/suggestions/ideas will be deeply appreciated.

Thanks in advance.

vy32
  • 28,461
  • 37
  • 122
  • 246
Mohammed Irfan
  • 93
  • 2
  • 2
  • 11

1 Answers1

1
 r.exec(new String[] {
    "cmd","/c", "start /home/pi/JARTest/JavaApplication24.jar", "file1" });

That is Windows code. It calls "cmd", the Windows command shell. So that won't work on other OS (like the Linux on your Pi).

Try to rewrite it using the "java" command.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I tried to do the same using the java command. It doesn't work either. I tried this : r.exec(new String[] {"java -jar /home/pi/JARTest/JavaApplication24.jar", "file1"}) – Mohammed Irfan Aug 02 '13 at 11:39
  • What errors are you getting? Maybe you need a full path to `java`? And try to separate the program name from the arguments ("java", "-jar /home...", "file1"); – Thilo Aug 02 '13 at 12:13
  • It doesn't give any error (thatss what is making it hard). Now it prints both the out.println's, but it doesn't give me the result of the execution of jar. – Mohammed Irfan Aug 02 '13 at 12:16
  • By doing this: out.println(r.exec(new String[] {"java", "-jar", "/home/pi/JARTest/JavaApplication24.jar"})); It prints me a Garbage value/ Junk value – Mohammed Irfan Aug 02 '13 at 12:18