Java Open Local file/exe/open URL/Call Cmd__java

Source: Internet
Author: User


* * Open File principle: Invoke cmd command to execute * * */import java.awt.BorderLayout;
Import Java.awt.EventQueue;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.border.EmptyBorder;
Import Javax.swing.JButton;
Import Java.awt.event.ActionListener;
Import java.awt.event.ActionEvent;

Import java.io.IOException;
 public class SS extends JFrame {private JPanel contentpane; public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {s
     s frame = new SS ();
    Frame.setvisible (TRUE);
    catch (Exception e) {e.printstacktrace ();
 }
   }
  });
  Public SS () {setdefaultcloseoperation (jframe.exit_on_close);
  SetBounds (100, 100, 450, 300);
  ContentPane = new JPanel ();
  Contentpane.setborder (New Emptyborder (5, 5, 5, 5));
  Setcontentpane (ContentPane);
  
  Contentpane.setlayout (NULL);
  JButton Btnnewbutton = new JButton ("\u6253\u5f00\u8ba1\u7b97\u5668"); Btnnewbutton.addactionlistener (new ActionListener () {publicvoid actionperformed (ActionEvent e) {try {//Open calculator runtime.getruntime (). EXEC ("calc");
     
     
    Use Notepad to open the local TXT file//runtime.getruntime (). EXEC ("notepad d:/help.txt");
    catch (IOException E1) {e1.printstacktrace ();
  }
   }
  });
  Btnnewbutton.setbounds (79, 60, 137, 23);
  
  Contentpane.add (Btnnewbutton);
  JButton Btnexe = new JButton ("\u8fd0\u884c\u672c\u5730exe"); Btnexe.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {try {//open local EXE
     
    Pieces can be absolute or relative path relative path in the engineering directory to create a folder Runtime.getruntime (). EXEC ("exe/registered patch");
    catch (IOException E1) {e1.printstacktrace ();
  }
   }
  });
  Btnexe.setbounds (79, 166, 137, 23);
  
  Contentpane.add (Btnexe); JButton btnnewbutton_1 = new JButton ("\u53ef\u6253\u5f00\u8fd0\u884c\u672c\u5730\u7684\u6587\u4ef6 \u4efb\u610f\
  u683c\u5f0f "); Btnnewbutton_1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {//Open any local file String cmd= "rundll32 url.dll fileprotocolhandler File://D:/mysqljdbc.png";
    try {Process p = runtime.getruntime (). exec (CMD);
    catch (IOException E1) {e1.printstacktrace ();
  }
   }
  });
  Btnnewbutton_1.setbounds (79, 117, 223, 23);
  
  Contentpane.add (btnnewbutton_1);
  JButton button = new JButton ("\u6253\u5f00\u7f51\u5740");
     Button.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {try {//Open URL
    Runtime.getruntime (). EXEC ("cmd/c start IExplore http://www.baidu.com");
    catch (IOException E1) {e1.printstacktrace ();
  }
   }
  });
  Button.setbounds (81, 214, 93, 23);
 Contentpane.add (button);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.