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: Launching a File

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Launching a File

    I am trying to launch a file when a button is pressed in my java program. I have read many solutions using the Runtime.getRuntime().exec() call but I can't get it working. I can't even get a file in the root c:\ directory to open. When I try the code:

    Runtime.getRuntime().exec("cmd /C start \"" + "C:/WORK.txt" + " \"");

    All it does is open a command prompt with the title "C:/WORK.txt". Can anyone suggest what might be wrong? Could this have anything to do with me not having administrator rights?


    Thanks,
    Geoff


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: Launching a File

    First of all, what exactly do you want to do, open the text file?

    Do something like this maybe.

    Runtime.getRuntime().exec("start C:/WORK.txt");

    // Json

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Launching a File

    Quote Originally Posted by Json View Post
    First of all, what exactly do you want to do, open the text file?

    Do something like this maybe.

    Runtime.getRuntime().exec("start C:/WORK.txt");

    // Json

    Yes, I am just trying to open the file. After it is open I don't care what happens to the file. I tried using
    Runtime.getRuntime().exec("start C:/WORK.txt");

    but I get a

    java.io.IOException: Cannot run program "start": CreateProcess error=2, The system cannot find the file specified



    And yes, the file is definitely there. I've tried accessing other files, in directories which I have access to write to and I still get the same thing.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: Launching a File

    start seems to be an invalid command.

    This works for me.

    Runtime.getRuntime().exec("notepad.exe C:/WORK.txt");

    // Json

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Launching a File

    Quote Originally Posted by Json View Post
    start seems to be an invalid command.

    This works for me.

    Runtime.getRuntime().exec("notepad.exe C:/WORK.txt");

    // Json

    When I try this Notepad opens, but it gives me an error stating "Cannot find the C:/WORK.txt file."

    Also I was just using the text file as an initial test, what I really want to accomplish is the opening of a .doc or .pdf file without having to specify the file path to Word or a PDF Reader exe (since this is going to be different on different systems).

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: Launching a File

    Ok, will this only be run on windows systems then I take it?

    // Json

  7. #7
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Launching a File

    Quote Originally Posted by Json View Post
    Ok, will this only be run on windows systems then I take it?

    // Json
    Yes. Do you have any ideas on what might be happening here?

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: Launching a File

    How about this then.

    Runtime.getRuntime().exec("cmd /c start c:/work.txt");

    // Json