1

Class Onetwo

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Onetwo extends JFrame

{
   JFrame f = new JFrame();
   JPanel p = new JPanel();    
   JButton b = new JButton("UG");    
   p.add(b);    
   f.add(p);    
   f.setSize(1030,740);    
   f.setVisible(true);    
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
}

class OpenFolder

public class OpenFolder {

    public static void main(String[] args) throws IOException 
    {            
            Desktop desktop = Desktop.getDesktop();        
            File dirToOpen = null;
            try {        
                String path = "e:\\Doss\\";        
                Runtime runtime = Runtime.getRuntime();        
                runtime.exec("explorer.exe "+path);        
                System.out.println("open");        
            } catch (Exception E) {        
                System.out.println("File Not Found");        
            }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3157505
  • 21
  • 1
  • 3
  • 1
    Fix your code example, it's completely broken – MadProgrammer Jan 21 '14 at 06:01
  • 1
    duplicate of : http://stackoverflow.com/q/5366976/1686291 – Not a bug Jan 21 '14 at 06:03
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Jan 21 '14 at 06:19

2 Answers2

3

here is an example for you:

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
    File file = new File("C:\\");
    Desktop desktop = Desktop.getDesktop();

    public Main() {
        super("open folder demo/SuRu");
        JButton button = new JButton("Open C:\\");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    desktop.open(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        add(button);
        setBounds(100, 100, 200, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }
}
SuRu
  • 739
  • 1
  • 6
  • 19
0

You can use this

String FolderName="C:/name";//Write your complete path here
try {
       Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + FolderName);
    } catch (IOException ex) {
             Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
    }
ravibagul91
  • 20,072
  • 5
  • 36
  • 59