Friday, 29 January 2016

[Java] When Command in Runtime.getRuntime().exec is Not Working

try {
    Runtime.getRuntime().exec( "echo Hello > test.txt");

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

If does not occur Exception and command is not working,

For Linux
try {
    Runtime.getRuntime().exec( new String[] { "/bin/sh", "-c", "echo Hello > test.txt"});

} catch ( IOException e) {
    e.printStackTrace();
}
For Windows
try {
    Runtime.getRuntime().exec( new String[] { "cmd.exe", "/c", "echo Hello > test.txt"});

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

No comments:

Post a Comment