?
Solved

executing a script from linux in Java

Posted on 1999-11-26
9
Medium Priority
?
302 Views
Last Modified: 2010-04-16
Hi,
I'm new to linux and I want to ask you how can I run a script written in linux ?
The script is "start0" and is working from linux environment. But when I execute the following method , it gives me some strange errors:
 
private void start(int nrCalc)
{
 try{
 Runtime runtime=Runtime.getRuntime();
 runtime.exec("/home/sionut/cafe/start0 "+nrCalc);
 }catch(IOException e)
 {
  out("An error with IO");
 }
}
 
Here's the script:
 
echo $1 >> /home/sionut/cafe/start.out
 
and the error is:
 
Reaped pid=14415, status =255
and then it gives me an error .
 
PS: Another question, not related to this. When I type "ps" and my program is running , it shows six times "java". My program doesn't start any child processes, so is it an error or it's normal ?
0
Comment
Question by:scutaru_ionut
  • 6
  • 3
9 Comments
 
LVL 5

Expert Comment

by:Jod
ID: 2237299
How are you calling the Start routine to create your process? And what is the error you get?

Do you normally get many java processes or do you normally just have one? the reason I ask is that the subprocess you have created is neccessarily killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. Your processes may be continuing...

The Process may also be being killed off or affected before finishing because your code does not wait for them. Maybe you need to wait? Worth trying to get it working.

Also remember that for a process started from java by default all its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()).

This leads on to a number of things to try based on retrieving the process object if you:

1) use waitfor() on the process object to wait until the process is finished

2) use exitValue() to see what the program return value is

3) use getOutputStream() on the process object to see what it is being produced


So in general try something more like this:

Runtime runtime = Runtime.getRuntime();
Process p = Runtime.exec("xxx");
p.waitFor();

until you have figured out what is going wrong. You will need to catch an exception on waitFor()...
0
 
LVL 5

Expert Comment

by:Jod
ID: 2237305
Here is a full example of reading the output of you script file:

import java.io.*;

public class CmdExec {

       public CmdExec(String cmdline) {
               try {
                       String line;

                       Process p = Runtime.getRuntime().exec(cmdline);
                       DataInputStream input = new DataInputStream(
                                                       p.getInputStream());
                       while ((line = input.readLine()) != null) {
                               System.out.println(line);
                       }
                       input.close();
               } catch (Exception err) {
                       System.out.println("EXEC failed: " + err.toString());
                       err.printStackTrace();
               }
       }

       public static void main(String argv[]) {
               new CmdExec(argv[0]);
       }

}
 

0
 

Author Comment

by:scutaru_ionut
ID: 2237504
The error I get is an SQL Exception which don't show up if I don't execute the script. It seems strange. I don't know why the catch block catches the exception, because the exception doesn't show up from SQL.
I'll try the waitFor() method ....
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 5

Expert Comment

by:Jod
ID: 2240197
SQL Exception?

There is more than meets the naked eye here....

You should not be getting an SQL exception unless you are doing something with SQL.

What elese are you doing in your code before or after calling the script?
0
 

Author Comment

by:scutaru_ionut
ID: 2240641
I made another simple test. Here it is:



import java.io.*;

public class Test
{
public Test()
{
  try{
  Runtime r=Runtime.getRuntime();
  Process p=r.exec("/bin/bash /home/sionut/cafe/start0 "+2);
  p.waitFor();
  }
  catch(IOException e)
  {
    System.out.println("Error 1");
   }
  catch(InterruptedException e)
  {
    System.out.println("Error 2");
  }
  catch(Exception e)
 {
    System.out.println("Error 2");
 }
}

public static void main(String args[])
{
      new Test();
}
}


And this program is working just sometimes. SOmetimes it just hangs, sometimes it gives me those strange messages (Reapid...) and sometimes works just fine. I'm using Linux 6.1
Also, the script in linux is working just fine and the path to it is good. Some people told me that I shoul call the script as an argument to the /bin/bash.
So ...
0
 
LVL 5

Expert Comment

by:Jod
ID: 2242235
import java.io.*;

public class Test
{
Runtime r = null;
 
public Test() {
  r=Runtime.getRuntime();
}

public runScript()
{
  try{
    Process p = r.exec("/bin/bash /home/sionut/cafe/start0 "+2);
    p.waitFor();

  } catch (Exception e) {
    System.out.println("Problems...\n" + e);
  }
}

public static void main(String args[])
{
  Test t = new Test();
  t.runScript();  
}

}

or even just:


public class Test
{


  public static void main(String args[]) {
    try {
      Runtime r=Runtime.getRuntime();

      // try with both these...

      //  Process p = r.exec("/home/sionut/cafe/start0 "+2);
      Process p = r.exec("/bin/bash /home/sionut/cafe/start0 "+2);

      p.waitFor();

    } catch (Exception e) {
      System.out.println("Problems...\n" + e);
    }
  }

}
0
 
LVL 5

Expert Comment

by:Jod
ID: 2242237
Try with the two examples above and tell me what happens...
0
 

Author Comment

by:scutaru_ionut
ID: 2242755
It's trhe same thing. Sometimes it works just fine, sometimes it works but gives me the Reapid .. thing and sometimes it just hangs up. But if I press CTRL+C to stop the process, the work is done, i.e. the number 2 is already in the file. But what is it waiting for ?
I think it's something about java or about Linux 6.1. I think I'll use something else..
0
 
LVL 5

Accepted Solution

by:
Jod earned 300 total points
ID: 2243327
Sounds like your process is not terminating for some reason. Any idea wht this might be?

Is there any other process looking at or locking the files you are trying to copy in your script?

It is probably not a java thing - I have never had a problem with exec.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question