0

I was trying to execute shell scripts using java code. The following is a sample code to demonstrate the issue :

ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/home/otaku/Programming/data/test1.sh");
try {

    Process process = processBuilder.start();
    StringBuilder output = new StringBuilder();
    BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

    String line;
    while ((line = reader.readLine()) != null) {
        output.append(line + "\n");
    }

    int exitVal = process.waitFor();
    if (exitVal == 0) {
        System.out.println(output);
    } else {
        System.out.println("Script exited abnormally");
    }

} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

The shell script file test1.sh that I am trying to execute :

#!/bin/bash
mkdir -p -- teeh
echo 'Succesfully executed script'

I am getting echo message and was able to print in the java console indicating that the shell script is executed successfully. But no directory is being created even though the command mkdir -p -- teeh is executed. If I manually execute the script file using terminal it works like a charm. I would like to know the reason behind this and a possible solution to this as well.

4
  • 1
    Getting the echo message is no guarantee that the mkdir command worked. it just show that yous script was called and did run. I heavily suspect a writing permission. You should be aware of the technical user who is executing java / sh and provide him write rights. Commented Dec 26, 2019 at 9:12
  • 2
    You might also want to add a pwd to your shell script or a System.out.println(System.getProperty("user.dir")); to your Java class to make sure you're actually looking at the correct location. You shouldn't expect the teeh directory to be created inside /home/otaku/Programming/data/ if you're not calling the java script from that location for instance. Commented Dec 26, 2019 at 9:44
  • @Aaron Thanks for notifying the location change as java code is being used to execute the script and so it depends on where my project folder lies. If you could write an answer then I could mark it is the answer. Commented Dec 26, 2019 at 10:27
  • @MehdiLAMRANI I have already made sure that it is not an issue with write permission. Commented Dec 26, 2019 at 10:28

1 Answer 1

2
mkdir -p -- teeh

In this command the teeh path is a relative one rather than an absolute one: it will be created in the script's current working directory.

Your bash script is by default executed with the working directory of your JVM, which depends on where you executed your java application from. If you're executing your code from your IDE, by default this will be the project's root directory. If you're executing from the command line, it will be the directory you execute the java command from.

In any case you shouldn't expect a /home/otaku/Programming/data/teeh directory to be created by your current code unless you run the java application from the /home/otaku/Programming/data/ directory.

There are many possible solutions, whose relevance depend on your context :

  • execute your java code from the /home/otaku/Programming/data/ directory
  • use an absolute path in your bash script
  • use cd in your bash script
  • use ProcessBuilder.directory(File dir) to execute the bash script with the appropriate working directory
Sign up to request clarification or add additional context in comments.

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.