How to create a folder with spaces written in Java under Linux?
Hello,
I have a serious problem
I want to run a Linux command using a Java class with the runtime interface, there is a command to create a folder named eg "My Folder", with a space
For create the Unix command is easy to do either:
mkdir My\ Folder
or
mkdir "My Folder"
But how to translate this in Java, I tried with two commands :
Runtime.exec("mkdir My\\ Folder")
Runtime.exec("mkdir \"My Folder\"")
Here is an example:
#
import java.io.IOException;
public class CreerDossier {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
runtime.exec("mkdir My\\ Folder");
runtime.exec("mkdir \"My Folder\"");
}
}
But it's still not working,
For runtime.exec("mkdir My\\ Folder") it creates two folders My\ and Folder
For runtime.exec("mkdir \"My Folder\"") it creates also two folders "My and Folder"
Are there solutions?
Thank you !
Re: How to create a folder with spaces written in Java under Linux?
If you have problems with the syntax that uses a single String for the command line, try using an array for the parts of the command line. The exec() method takes an array for an arg
Re: How to create a folder with spaces written in Java under Linux?
I have not understood enough,
can you give me an example please?
Thanks for your response
Re: How to create a folder with spaces written in Java under Linux?
Create an array with each item of the command line in one element of the array:
Code :
String[] cmdLine = {"the command", "arg1 for the command", "arg2 for the command"};
...
...exec(cmdLine);
Re: How to create a folder with spaces written in Java under Linux?
Thanks, it is clear know,
how we use your method for this command :
"chmod 777 My\ Folder" ?
that is :
String[] cmdLine = {"chmod", "777", "My Folder"};
...exec(cmdLine);
Thank you !
Re: How to create a folder with spaces written in Java under Linux?
Did it work as you wanted?
Re: How to create a folder with spaces written in Java under Linux?
I would like to know why you need to run an OS-specific command? The point of Java is that it is not system-dependent.
There are Java commands to create a directory already.
Re: How to create a folder with spaces written in Java under Linux?
It's OK, with :
String[] cmdLine = {"chmod", "777", "My Folder"};
...exec(cmdLine);
thank you a lot :)