1

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

  1. See CMD.exe (Command Shell) | Windows CMD | SS64.com
  2. See Start - Start a program | Windows CMD | SS64.com
  3. The title ("Title") is not required.
Paul Vargas
  • 73
  • 1
  • 1
  • 5
  • we have SO MANY ways to execute a file in linux. but perhaps sharing the contents of your .bat file would be more lucrative. the path, C:\ doesnt exist, youll change that to the path in linux. ~/ – j0h Mar 25 '15 at 06:15
  • @j0h I have edited the question. The content of the script can be anything. However, I am interested in the command to launch it with a similar behavior. – Paul Vargas Mar 25 '15 at 06:53
  • The question effectively is a duplicate of this: http://askubuntu.com/q/38661/295286 Once bounty expires, it should be voted to be closed – Sergiy Kolodyazhnyy Apr 04 '17 at 12:32
  • @SergiyKolodyazhnyy - the item you link is about running scripts from the desktop. OP asks about launching processes from within Java. Not a duplicate? – tk-noodle Apr 05 '17 at 22:12
  • @taifwa OP knows how to launch process from Java program - they already mention Runtime.getRuntime().exec(), and Java being cross-platform language would use same method to launch a program. The post isn't about running scripts form desktop either - it's running scripts in general, and the key to that making script executable with chmod +x and giving path to script or at least having script live in one of the directories that belong to PATH variable. Whether or not it's form Java is irrelevant. These steps are required – Sergiy Kolodyazhnyy Apr 05 '17 at 22:21

4 Answers4

5

The way to launch a script is to give its path:

/path/to/script.sh

To 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.

terdon
  • 100,812
1

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
0x2b3bfa0
  • 8,780
  • 6
  • 36
  • 55
1

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.bat

where its contents are:

$ cat foo.bat 
#!/bin/bash
echo -e "Having a bash" $0

I think the use of $0 can give you the title functionality you may desire. Many more possibilities exist.

j0h
  • 14,825
  • 2
    gnome-terminal and xterm are not shells themselves, rather they're terminal emulators which run a shell. bash is a shell. zsh is a shell. ksh is a shell. – hanetzer Mar 25 '15 at 14:52
1

As 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?

tk-noodle
  • 788
  • 3
  • 15