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,

John

My code:

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

/*
 * NewJFrame.java
 *
 * 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() {
        initComponents();
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(154, 154, 154)
                .add(jButton1)
                .addContainerGap(173, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(86, 86, 86)
                .add(jButton1)
                .addContainerGap(191, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

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

        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)
 {
  exception.printStackTrace();
 }  }


    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                   

}

I suggest taking a look at the JFileViewer class.

Use following code

String a="C:\\csnet";
  Runtime.getRuntime().exec("cmd /c start "+a);

Thank you so much, it works great. :)

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 {
	desktop.open(file);
} 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.