Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to create a folder with spaces written in Java under Linux?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 !


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    String[] cmdLine = {"the command", "arg1 for the command", "arg2 for the command"};
    ...
     
    ...exec(cmdLine);
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 !

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to create a folder with spaces written in Java under Linux?

    Did it work as you wanted?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default 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.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. JAVA code for deleting spaces from Text file
    By Maheshgx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 17th, 2012, 06:26 AM
  2. Trying to create Triangles with Characters and Spaces
    By MJtechie12 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 20th, 2011, 04:20 PM
  3. Replies: 1
    Last Post: June 7th, 2011, 11:53 AM
  4. printing in java; missing spaces
    By user85116 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 02:51 PM
  5. create the project folder
    By vel024 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 3rd, 2010, 11:27 AM