tjgquicken
asked on
Java and Darwin
I have a Java applet that runs on Mac OS X and the underlying Darwin interface. The applet makes the following system call:
Runtime.getRuntime().exec( "ditto -c -k --keepParent -rsrc " + srcFile + " " + destFile).
This system call works fine if there are no spaces in srcFile, but (of course) it doesn't work if there's a space. My problem is that when I put quotes around srcFile, like
Runtime.getRuntime().exec( "ditto -c -k --keepParent -rsrc \"" + srcFile + "\" " + destFile) or Runtime.getRuntime().exec( "ditto -c -k --keepParent -rsrc '" + srcFile + "' " + destFile)
that doesn't work either. The ditto command creates destFile, but it's empty.
The problem isn't with the ditto command, because I can use Terminal and type
ditto -c -k --keepParent -rsrc "/Users/computer/Desktop/t est file.qdfm" /tmp/file1.tmp or
ditto -c -k --keepParent -rsrc '/Users/computer/Desktop/t est file.qdfm' /tmp/file1.tmp
and both work as expected. Does anybody know what I'm doing wrong?
Thanks,
Jay
Runtime.getRuntime().exec(
This system call works fine if there are no spaces in srcFile, but (of course) it doesn't work if there's a space. My problem is that when I put quotes around srcFile, like
Runtime.getRuntime().exec(
that doesn't work either. The ditto command creates destFile, but it's empty.
The problem isn't with the ditto command, because I can use Terminal and type
ditto -c -k --keepParent -rsrc "/Users/computer/Desktop/t
ditto -c -k --keepParent -rsrc '/Users/computer/Desktop/t
and both work as expected. Does anybody know what I'm doing wrong?
Thanks,
Jay
Does it work when there are *no* spaces?
taken from:
https://www.experts-exchange.com/questions/22743766/Cannot-open-a-file-using-Runtime-getRuntime-exec-cmd-c-start-filename-in-java.html
OK here is what im testing and it works for me:
/*
* Main.java
*
* Created on 06 August 2007, 13:19
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author sdelaney
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String filename ="W:/Java/JavaApplication1 /raymond questions.txt";
Runtime rt = Runtime.getRuntime();
final Process proc = rt.exec(
new String[] {
"cmd.exe",
"/c",
"\"\""+filename+"\"\""
}
);
} catch(Throwable cause) {
}
}
}
https://www.experts-exchange.com/questions/22743766/Cannot-open-a-file-using-Runtime-getRuntime-exec-cmd-c-start-filename-in-java.html
OK here is what im testing and it works for me:
/*
* Main.java
*
* Created on 06 August 2007, 13:19
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author sdelaney
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String filename ="W:/Java/JavaApplication1
Runtime rt = Runtime.getRuntime();
final Process proc = rt.exec(
new String[] {
"cmd.exe",
"/c",
"\"\""+filename+"\"\""
}
);
} catch(Throwable cause) {
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, it works correctly when there are no spaces. I'm going to try the exec command with the String array parameter and report back.
ASKER
The overloaded exec command worked. Thanks.
:-)