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
11
  • Why should Eclipse matter? That's an IDE. Commented Jan 9, 2017 at 10:29
  • Please, format your code. Is there line break after changing directory? Commented Jan 9, 2017 at 10:34
  • I am automating one web app , which need server to start , so I need to run this command. Commented Jan 9, 2017 at 10:35
  • 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. Commented Jan 9, 2017 at 10:36
  • 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. Commented Jan 9, 2017 at 10:47

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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
See Update. "/Users/sujitsoni/Documents/workspace/ElectronApplication" looks like a Windows System but you are saying it is a Base script on a UNIX.
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);
    
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.