Results 1 to 4 of 4
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Unhappy how to run file fron JFileChooser ? ( need small help)

    Hi,
    i want to open and run the .jpg/.txt files from any folder by using JFileChooser..

    I almost done with my code without error but its not running..
    Can anybody pls help me to figure out where need change ?

    Java Code:
    	JFileChooser fc = new JFileChooser(".");
    	fc.setFileSelectionMode(JFileChooser.FILES_ONLY ); 
    	
    	int returnVal = fc.showOpenDialog(null);
    
                   if (returnVal == JFileChooser.APPROVE_OPTION) {
      
     		File selectedFile = fc.getSelectedFile();
    		try
             {
    	Runtime r=Runtime.getRuntime();
    
    String[] cmd={"cmd", "/c", "start" +fc.getSelectedFile()};
    Process P=r.exec(cmd);
    
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    System.exit(0);
    }
    Last edited by doha786; 01-29-2010 at 04:40 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by doha786 View Post
    Java Code:
    String[] cmd={"cmd", "/c", "start" +fc.getSelectedFile();
    Have a close look at that last argument: it concatenates the String "start" to the name of the selected file; is that really what you want?

    kind regards,

    Jos

  3. #3
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default

    thanks 4ur reply...

    no, i dont want to concatenate the String. Actually i just copy this method from one book directly without understanding.

    right now, i just simply want to run any single selected file .....

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by doha786 View Post
    thanks 4ur reply...

    no, i dont want to concatenate the String. Actually i just copy this method from one book directly without understanding.

    right now, i just simply want to run any single selected file .....
    Have a look at the API documentation for the DeskTop class because you want to find the program associated with a non runnable file (e.g. .txt or .jpg). Pass the file name to this class and it will find an executable file for you and run it given the file name as an argument.

    kind regards,

    Jos

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 10:17 PM
  2. Replies: 0
    Last Post: 11-28-2009, 10:02 AM
  3. Replies: 1
    Last Post: 11-13-2008, 07:43 AM
  4. how to use JFileChooser to select file -> string
    By gezzel in forum AWT / Swing
    Replies: 15
    Last Post: 10-23-2008, 05:34 AM
  5. how to use JFileChooser to select file -> string
    By gezzel in forum New To Java
    Replies: 9
    Last Post: 09-18-2008, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •