1

I have a script, which is test.sh on Ubuntu. I want to run it from Java. I know I have to use Runtime.getRuntime().exec();

Don't I have to fill the exec parenthesis with the location of test.sh? I am typing /home/main/ss/test.sh

I do not get any error messages but when I searched the folder, I saw that script did not work. How can I fix it?

9
  • Can you post the code that failed? Commented Feb 6, 2012 at 21:17
  • Actually my script works correct, but when I try to work it from java, I do not get any response Commented Feb 6, 2012 at 21:18
  • I mean that when I work it on console, I get the right result Commented Feb 6, 2012 at 21:20
  • 1
    probably duplicate stackoverflow.com/questions/525212/… Commented Feb 6, 2012 at 21:36
  • 1
    How do you know that your script failed? Commented Feb 7, 2012 at 1:20

3 Answers 3

1

You should really look at Process Builder. It is really built for this kind of thing.

 ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 Process p = pb.start();
Sign up to request clarification or add additional context in comments.

3 Comments

But I do not understand how this can work. Is this just an example?
You can put the path to your sh file in place of myshellScript.sh and arguments (if any) after that.
I do not give any argument. For example, if I write these on java, is it gonna work? ProcessBuilder pb = new ProcessBuilder("/home/myshellScript.sh"); Map<String, String> env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(new File("myDir")); Process p = pb.start();
0

Did you try to call your script directly, or did you specify a shell to use? You may need to specify the shell on your exec call if it's not specified (and processed) from the first line of the script.

3 Comments

I have tried to call my script directly. I have written just this : Runtime.getRuntime().exec("/home/main/ss/test.sh");
What shell is it written for? You should probably be doing something similar (but could be different depending on your shell/os/environment requirements) to: Runtime.getRuntime().exec("/bin/bash /home/main/ss/test.sh");
If there is a shebang in the script (#!/bin/bash), you don't need to specify the shell, to pick the right one, because the kernel evaluates the shebang. Is there such a shebang?
0

From looking at your description, how you conclude the script didn't work:

I do not get any error messages but when I searched the folder, I saw that script did not work. How can I fix it?

I conclude, the script creates a new file, and there is none, after starting from Java.

Maybe you assume, that the output of the script will, if sent to the current directory, end in the directory of the script, not in the directory where you started your Java application.

This might be the Desktop, if you start your class from the UI with an Icon as starter, it might be your home or project dir, if you compile the class from hand, or a directory, set up by your IDE, if you develop with eclipse or the like.

If your script writes to an absolute path like /tmp/script.out, you could verify fast, whether this might be your issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.