I have a Java program that runs a batch file on Windows with Runtime.getRuntime().exec() using the following command:
cmd /C start "Title" "C:\Folder\file.bat"The Java program ends quickly since cmd /C carries out the command and then terminates (1) and start "Title" "C:\Folder\file.bat" starts the batch script (2). Thus the process (the batch file) will continue running independently.
Now, suppose that I have an shell script (e.g. file.sh), which I want to launch from Java and has a similar behavior. How could it be the equivalent command (3) in Linux?
Notes
- See CMD.exe (Command Shell) | Windows CMD | SS64.com
- See Start - Start a program | Windows CMD | SS64.com
- The title (
"Title") is not required.
4 Answers
The way to launch a script is to give its path:
/path/to/script.shTo have it continue if the parent process exits, you can just launch it in the background by appending &:
/path/to/script.sh &So, if you have, for example, one script calling another like so:
#!/bin/bash
script2.sh &You could run script1.sh which will call script2.sh and, because it is sent to the background, exit immediately.
Various ways to execute a script in Linux: my arbitrary script name is foo.bat
ex1:$bash foo.bat
ex2:$./foo.bat
ex3:$echo $(~/Desktop/foo.bat)
ex4:`~/Desktop/foo.bat`
ex5:$ /path/to/shell/script/foo.bat
ex1: this is is just a regular file at this point, we execute it with bash ex2: we have marked foo.bat as executable. ($chmod +x foo.bat) ex3: Command substitution ex4: execute using "``" ex5: we us the path to the executable.
These are the most common. I would avoid calling things like gnome-terminal, and xterm. These are less common shells compared to things like bash, and sh. For example I use Ubuntu mate. I have neither xterm nor gnome-terminal. But, I do have bash, and sh. as does nearly ever other person running Linux / mac osx.
You may appreciate this tutorial on script execution here
the script foo.bat does this:
$ ./foo.bat
Having a bash ./foo.batwhere its contents are:
$ cat foo.bat
#!/bin/bash
echo -e "Having a bash" $0I think the use of $0 can give you the title functionality you may desire. Many more possibilities exist.
1As I understand, you want to run an external command from within Java, and have the Java process continue (asynchronously) whilst that external command continues to run too?
Here's a little test I whipped up:
[tai@flenux runproc ] $ ls
filecreator.sh ProcRunner.class ProcRunner.java
[tai@flenux runproc ] $ cat filecreator.sh
touch newfile
[tai@flenux runproc ] $ cat ProcRunner.java
public class ProcRunner { public static void main(String[] args) { try { Runtime.getRuntime().exec(args); Thread.sleep(2000); System.out.println("Done"); } catch(Exception e) { System.err.println(e.toString() ); } }
}
[tai@flenux runproc ] $ java ProcRunner bash filecreator.sh
Done
[tai@flenux runproc ] $ ls
filecreator.sh newfile ProcRunner.class ProcRunner.java
[tai@flenux runproc ] $ Java successfully execs the program (which simply creates the "newfile" file) ; it also continues on its own merry way to print the Done message after calling the exec.
If you wanted a terminal window to open too, prefix the call string
Runtime.getRuntime().exec(["gnome-terminal", "-e"]+args);So to answer your question, it looks like you would just call the method you specified. Are you getting a different behaviour?
You have many options, the more interesting are these:
If it's for Ubuntu, you can use
gnome-terminal:gnome-terminal -c "/home/$USER/file.sh" -t "Title"or:
gnome-terminal -- "/home/$USER/file.sh" -t "Title"If do you want to make it compatible with most linux, you can use
xterm:xterm -T "Title" -e "/home/$USER/file.sh"
The path is /home/$USER/file.sh (Windows 7 equivalent: C:\Users\%UserProfile%\file.bat)
The file.sh contents may be something like this:
#!/bin/bash
echo "In a World without Walls nor Fences, the people don't need Windows and Gates"
sleep 2