Good evening,

I am trying to simply open a folder located on my local C:\ drive, to be exact just for testing purposes I am trying to open C:\temp folder.
What I have is a JFrame with a single Jbutton and some code, however when I execute the button, nothing absolutely nothing happens, it doesn't crash, it doesn't open a folder, it doesn't give me a message error.

line 65 to 85 is where my code resides.
under the "private void jButton1ActionPerformed" I call a "OpenMain();" method which I define right under that starting on line 69.

I would definitly appreciate some help,


My code:

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

 * Created on Jul 4, 2009, 6:57:23 PM

package systems;

 * @author olivier
public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();


        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
                .add(154, 154, 154)
                .addContainerGap(173, Short.MAX_VALUE))
                .add(86, 86, 86)
                .addContainerGap(191, Short.MAX_VALUE))

    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        private void OpenMain()
    { try
  //Path for folder or file that you want to open
  //In my case i will open C:\ drive
  //So create a String like below
  //Don't forget to change \ to \\
  String a="C:\\temp";

  //Execute command
  //Command that you must know : rundll32 SHELL32.DLL,ShellExec_RunDLL File_Or_Folder_To_Open
  Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+a+"\"");
 catch(Exception exception)
 }  }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   


Here is another way to open folder or directory

String path = "C:\\"; // path to the directory to be opened
File file = new File(path);
Desktop desktop = Desktop.getDesktop();
try {;
} catch (IOException e){ }

to use Desktop you must import java.awt.Desktop.

This method can be little-bit complex. But it works well. You can use any.

Edited 6 Years Ago by ankurgecr: n/a

This question has already been answered. Start a new discussion instead.