2

I need to start a server using bash, so I had created an UNIX shell , but I am not able to execute it with Java from Eclipse.

I tried the following code which doesn't work :

Process proc = Runtime.getRuntime().exec(./startServer);

Here is content of the startServer file :

#!/bin/bash
cd /Users/sujitsoni/Documents/bet/client
npm start
Aaron
  • 24,009
  • 2
  • 33
  • 57
cod
  • 151
  • 4
  • 13
  • Why should Eclipse matter? That's an IDE. – duffymo Jan 09 '17 at 10:29
  • Please, format your code. Is there line break after changing directory? – Andrii Abramov Jan 09 '17 at 10:34
  • I am automating one web app , which need server to start , so I need to run this command. – cod Jan 09 '17 at 10:35
  • @AndriiAbramov : yes – cod Jan 09 '17 at 10:36
  • 1
    When you say it doesn't work, what actually happens? If an error is displayed you should definitely tell us which it is, and if execution stops without any error you should say so. – Aaron Jan 09 '17 at 10:36
  • @Aaron: No there is no error – cod Jan 09 '17 at 10:37
  • Does execution stop, or does it carry on running without any visible effect? – Aaron Jan 09 '17 at 10:38
  • Also as a first debugging step I'd add a simple `echo something` to the beginning of the `startServer` script to check whether it is actually called but does nothing or isn't called. – Aaron Jan 09 '17 at 10:41
  • Of course you should also make sure the `startServer` script works when called from your shell, but I suppose you've already done that. – Aaron Jan 09 '17 at 10:45
  • 1
    If you use `"./startServer"` the script must be in the same directory as the current java directory. This can be a bit hard to guess depending on how you start it (In case of Eclipse it is most likely the project root). I would use an absolute ath to the script instead. – eckes Jan 09 '17 at 10:47
  • @eckes I upvoted your comment because it is a probable cause of the problem, however I'd argue against using an absolute path in case the project is to be shared (or used on different computers). It would be better to correctly understand how the relative path works in that case since the absolute path might not always be the same. – Aaron Jan 09 '17 at 10:49

2 Answers2

2

You can try the following two options.

Option 1

Process proc = Runtime.getRuntime().exec("/bin/bash", "-c", "<Abosulte Path>/startServer");

Option 2

ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "<Absolute Path>/startServer");
pb.directory(new File("<Absolute Path>"));
Process proc = pb.start();
shazin
  • 21,379
  • 3
  • 54
  • 71
  • java.io.IOException: Cannot run program "/Users/sujitsoni/Documents/workspace/ElectronApplication/startServer" (in directory "/Users/sujitsoni/Documents/workspace/ElectronApplication/startServer"): error=20, Not a directory – cod Jan 09 '17 at 11:02
  • See Update. "/Users/sujitsoni/Documents/workspace/ElectronApplication" looks like a Windows System but you are saying it is a Base script on a UNIX. – shazin Jan 09 '17 at 11:09
2

A couple Of things can go wrong:

  • The path to the file you have given might be wrong for eclipse it can take relative path but from the command line, it will take the absolute path.

  • error=13, Permission denied - If the script file doesn't have required permissions. In your scenario, that might not the case as you are not getting any error.

  • At last, you are executing the script by java program so the output of your script will not be printed out. In your scenario, this might be the case. You need to capture the output of script from BufferedReade and print it. ( In your case server might have started but you are not seeing the logs/output of the script. See the code sample below for printing output.

    public static void main(String[] args) throws IOException, InterruptedException {
    
           Process proc = Runtime.getRuntime().exec("./startServer");
           proc.waitFor();
           StringBuffer output = new StringBuffer();
           BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
           String line = "";
           while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
           }
           System.out.println(output);
    
    }
    
Community
  • 1
  • 1
Gopinath Langote
  • 311
  • 4
  • 10