2

Possible Duplicate:
Java code to execute a .sh file

I write the code in a .sh file with one parameter in /root/new_scripts/setpermission.sh and run it: setpermission.sh contains:

chmod 777 $1

It executed successfully from linux console by typing /root/new_scripts/setpermission.sh location . But when i tried to run it using java code using: Java full code:

String fileLocation = BASE_DIR + domain + SUB_DIR_CAKE + fileName;
    File newFile = new File(fileLocation);
    System.out.println("Permission file location: " + fileLocation);
    if(newFile.exists()) {
        String command;
        String[] commandArray;
        command = "/root/new_scripts/setpermission.sh";
        File commandFile = new File(command);
        if(commandFile.exists()) {
            System.out.println("\n\n\n\n\nFILE EXISTS");
        } else {
            System.out.println("\n\n\n\n\nFILE NOT EXISTS");
        }
        commandArray = new String[]{"/bin/sh", command, newFile.toString()};
        try {
            Process p = Runtime.getRuntime().exec(commandArray);
            return "HERE OK";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    } else {
        return "file not exists";
    }

and it returns HERE OK

1
  • Do you get the "execution error" or is just nothing happening? Commented Dec 5, 2011 at 3:47

2 Answers 2

3

Please post the Java code that you're using to attempt this. However, by making a few assumptions, you probably need to find and use the fully-qualified path to "sh", as the Java ProcessBuilder won't make use of a set "PATH" environment variable.

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

11 Comments

got it? i have added java code.
So his answer is 100% correct ... you need to provide the full path to sh
./root/new_scripts/setpermission.sh provides full path, but not working.
Is your . in "./root" literal, or a typo? If literal, this won't work either. You would need an absolute, full path to the script. (Symbolic links would be o.k.)
I was asking if you had a typo - and since it matches what's now in your edit, I'm assuming it's not a typo. That said, you now have 2 problems: You need to specify the full path to "chmod", as well as to your .sh script. ("./root/..." is not an absolute path.) A proper absolute path will begin with "/".
|
1

try this:

            String command;
            String[] commandArray;


            command = "./new_scripts/setpermission.sh";
            commandArray = new String[]{"/bin/sh", command, fileLocation, permission};
          try {

              Process p = Runtime.getRuntime().exec(commandArray);

         } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

24 Comments

Hey, you need to know that root is one of my folder as new_scripts. Anth its not working for me, any suggestion please?
new_scripts is your working directory right then you need to give in command only "./setpermission.sh" or "setpermission.sh"
No man, my working directory is elsewhere, any place, not specified.
then where u put your sh file??? give that whole path from root
String command; String[] commandArray; command = "/root/new_scripts/setpermission.sh"; commandArray = new String[]{"/bin/sh", command, fileLocation, permission}; try { Process p = Runtime.getRuntime().exec(commandArray); return "HERE OK"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.