0

I am in some trouble. I am trying to force create a folder using bash command through java code in linux server. My code is as followes-

String command = "/root/new_scripts/makedir.sh /webroot/Own";
    try {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        return "true";
    } catch (Exception ex) {
        return ex.toString();
    }

and the makedir.sh files contains

#!/bin/bash
mkdir $1

But it cannt create a directory.

And also try to create a directory just using java code is as following-

String s = null;
    try {
        Process p = Runtime.getRuntime().exec("mkdir /webroot/Own");
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read the output from the command
        System.out.println("\n\n\nHere is the standard output of the command:\n");
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    } catch (Exception ex) {
        System.out.println("\n\n\nexception happened - here's what I know: ");
        ex.printStackTrace();
    }

But it gives me the following error-

Here is the standard output of the command:

Here is the standard error of the command (if any):

mkdir: cannot create directory `/webroot/Own': Permission denied

6
  • Please, capture the output of the process to stdout and stderr and provide it in your post as well. And process exit code as well. Commented Dec 6, 2011 at 8:47
  • Hint: you don't need to call the external script, as mkdir is an executable in Linux. Simply use command mkdir /webroot/v3custompritom. Commented Dec 6, 2011 at 8:49
  • String command = "mkdir /webroot/v3";// try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); } catch (Exception ex) { } Commented Dec 6, 2011 at 8:52
  • Trsy strace-ing your Java VM; you'll understand why your mkdir fails. Commented Dec 6, 2011 at 11:41
  • 1
    @Pritom: You haven't updated your question as I've asked in my first comment. Checkout the same advise here, and solutions here and more correct there. Commented Dec 6, 2011 at 19:28

3 Answers 3

2

Why don't you use the mkdir function of Java which calls directly the mkdir system call?

It probably fails because of permission issues.

Sign up to request clarification or add additional context in comments.

1 Comment

If the syscall failed, it gave you an error code explaining why.
1

try changing your project directory permissions: chmod +w /<your project directory> then use mkdir command.

1 Comment

That would not possible, Have please any other idea to force create a directory like force delete directory?
1

Handling the output of a process in Java is not the easiest thing to do. If it is for debugginh only, I would modify makedir.sh like so;

mkdir $2 > err.txt 2>err.txt

Run your java code and then check err.txt- expect to find something like permission denied in there. For production code, you would like your java app to be aware of the failure of makedir.sh. Look at this great article for tips on how to read the process' stream correctly.

1 Comment

It is for permission denied, but is there any way to force create the folder?

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.