Java in the desktop class and bulk in browser open URI

Source: Internet
Author: User

There are more than one page URL, I need to see if the Web page is normal, I recently found that it hurts, I want to copy the URL one by one, put into the address bar, this once the URL of a number, really hurt, the beginning do not know how to start, refer to the http://www.jb51.net/article/ 42460.htm this article, and then went back to see the API.


The first method is to write the timer tool to use something.

Runtime.getruntime (). EXEC ("rundll32 Url.dll,fileprotocolhandler http://www.csdn.net");

with the operation of Windows instructions, the runtime executes the same method as you do in a DOS window. But Rundll32 Url.dll's instructions like this don't really know.


The second approach is to use the desktop class provided by Java.

Import Java.awt.desktop;import java.io.file;import Java.io.ioexception;import Java.net.uri;import java.net.urisyntaxexception;/** * @author CKC * Follow API to open URI default message open Send to URI * Default program Open edit File */public class Testdeskt op {public static void main (string[] args) {//desktop object is created by Getdesktop to get the instance of Desktop dt = Desktop.getdesktop ();// Determine if the desktop class supports the current platform if (dt.isdesktopsupported ()) {System.out.println ("Your platform supproted desktop class!");} Desktop.action is an enumeration type with browse edit open mail Print these system operations///The following method can determine whether the operation is supported System.out.println (dt.issupported ( Desktop.Action.MAIL)); System.out.println (dt.issupported (Desktop.Action.EDIT)); System.out.println (dt.issupported (Desktop.Action.OPEN)); System.out.println (dt.issupported (Desktop.Action.BROWSE)); System.out.println (dt.issupported (Desktop.Action.PRINT));//The default is Notepad file Text = new File ("E:/test.txt"); try {dt.edit ( text);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} JSP defaults to Sublime TEXT2 does open the file JSP = new file with the editor ("E:/teSt.jsp "); try {Dt.open (JSP);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} The printing of an IO exception connected to the device on the system does not work should be a computer without a printer relationship try {dt.print (JSP);} catch (IOException e) {//TODO auto-generated catch Blocke. Printstacktrace ();} The default browser is used to open the URI uri;try {uri = new Uri ("Www.csdn.net");d T.browse (URI);} catch (URISyntaxException e) { E.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}}

after this is done, the code is modified to open the URI only in bulk for the browser.

Import Java.awt.desktop;import java.io.ioexception;import Java.net.uri;import java.net.urisyntaxexception;public Class Main {public static void Test (String uristring) {try {uri uri = new Uri (uristring);D esktop dp = Desktop.getdesktop () ; if (dp.issupported (Desktop.Action.BROWSE)) {Dp.browse (URI);}} catch (IOException e) {System.out.println ("Browser open URI Fail"),} catch (URISyntaxException e) {System.out.println (" URI is illegal ");}}}

because of the many changeable URIs, it is not possible to write to a string array in the program, the best way is to use the graphical interface, directly with the swing of the previously written tools to modify.


Import Java.awt.borderlayout;import java.awt.container;import Java.awt.dimension;import Java.awt.FlowLayout;import Java.awt.gridlayout;import Java.awt.point;import Java.awt.event.actionevent;import Java.awt.event.ActionListener; Import Java.io.bufferedreader;import java.io.file;import java.io.filenotfoundexception;import java.io.FileReader; Import Java.io.ioexception;import java.util.arraylist;import Java.util.list;import Javax.swing.box;import Javax.swing.boxlayout;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JLabel;import Javax.swing.jpanel;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;import Javax.swing.JTextField; Import Javax.swing.event.documentevent;import javax.swing.event.documentlistener;/** * @author CKC 2015-4-10 14:39:13 * The current version is for the latest http://download.123cw.cn/AppDownload/* app/singlegame/mm/bydr401.apk Enter a set of URLs after letting the system default browser Firefox will They open each other. * Desktop This class is good to use * * */public class Testsinglegame {//Two listeners need to use the variable-two text field oneInput an output to place it in a global variable static jtextarea text = new JTextArea (+), static JScrollPane outjsp = new JScrollPane ();p ublic static void Main (string[] args) {JFrame frame = new JFrame ("Openurlinexplorer");//number of rows, number of columns borderlayout layout = new BorderLayout () ;//form size, form position frame.setsize (+); Frame.setlocation (400, 200); Container Container = Frame.getcontentpane (); container.setlayout (layout); JTextField field = new JTextField ("URL address: Copy address here and press start to run"); Field.seteditable (false);//int rows int columns// JTextArea Text = new JTextArea (15,80);//Text.setlinewrap (TRUE); When there is no scroll bar true for the outside of the text box section also counts to add jscrollpane jsp = new JScrollPane (text) of the////scrollbar,//int rows int columnsbox box = Box.createvert Icalbox (); JPanel p = new JPanel (); BoxLayout blayout = new BoxLayout (p, Boxlayout.y_axis);p. setlayout (Blayout);p. Add (field);p. Add (JSP);// Frame.setcontentpane (P);//frame.setlayout (layout); JPanel P2 = new JPanel (); BoxLayout blayout2 = new BoxLayout (P2, Boxlayout.y_axis);p 2.setLayout (blayout2);p 2.add (outjsp); JButton B = New JButton ("Start to open every URI at your default browser"), B.addactionlistener (new ActionListener () {public void acti Onperformed (ActionEvent e) {///N for newline spaces string skip directly over string[] array = Text.gettext (). Split ("\ n"); for (int i = 0; i < Array.len Gth i++) {main.test (array[i]);}}); Container.add (P, "North"); Container.add (b, "Center");//Frame.pack (); frame.setresizable (false); Frame.setvisible ( true); Frame.setdefaultcloseoperation (Jframe.exit_on_close);}}

The final result:


Enter the URL into the text box, and press execute to open it in the default browser.

Java in the desktop class and bulk in browser open URI

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.