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

Pritom
  • 1,294
  • 8
  • 19
  • 37
  • 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. – dma_k Dec 06 '11 at 08:47
  • Hint: you don't need to call the external script, as mkdir is an executable in Linux. Simply use command `mkdir /webroot/v3custompritom`. – dma_k Dec 06 '11 at 08:49
  • String command = "mkdir /webroot/v3";// try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); } catch (Exception ex) { } – Pritom Dec 06 '11 at 08:52
  • Trsy `strace`-ing your Java VM; you'll understand why your `mkdir` fails. – Basile Starynkevitch Dec 06 '11 at 11:41
  • 1
    @Pritom: You haven't updated your question as I've asked in my first comment. Checkout the same advise [here](http://stackoverflow.com/a/3209770/267197), and solutions [here](http://www.devdaily.com/java/edu/pj/pj010016) and more correct [there](http://stackoverflow.com/a/3350862/267197). – dma_k Dec 06 '11 at 19:28
  • Hey man, I have edited my question, please take a look at it. It does not give me permission to create a directory, but I want to create a directory forcely. – Pritom Dec 07 '11 at 02:34

3 Answers3

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

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

Chadwick
  • 12,555
  • 7
  • 49
  • 66
dj buen
  • 208
  • 1
  • 9
  • That would not possible, Have please any other idea to force create a directory like force delete directory? – Pritom Dec 06 '11 at 09:08
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.

mbatchkarov
  • 15,487
  • 9
  • 60
  • 79