Java program opens the specified address Web page

Source: Internet
Author: User

1, today encountered the need to manually enter the HTTP address to open the specified Web page needs, try to do a program to open the specified Web page function, search, There really is a ready-made example, a little transformation, to achieve their own needs;

2, The code is not many, two files, as Follows:

 packagecom.lgp.solr;Importjava.io.BufferedReader;Importjava.io.FileNotFoundException;Importjava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List; public classJavafile { public StaticList<string>getUrl (String Path) {List<String> URLs =NewArraylist<string>(); Try{filereader Reader=NewFileReader (path); BufferedReader BR=NewBufferedReader (reader); String Str=NULL;  while(str = Br.readline ())! =NULL) {                if(str!=NULL&& Str.startswith ("http") ) {urls.add (str);            }} Br.close ();        Reader.close (); } Catch(filenotfoundexception E) {e.printstacktrace (); } Catch(ioexception E) {e.printstacktrace (); }        returnurls; }}

The primary function of this class is to read the URL address in the specified file, read by line, and filter the line content that begins with http;

 packagecom.lgp.solr;///////////////////////////////////////////////////////////Bare Bones Browser Launch////Version 1.5 (december, 2005)////by Dem Pilafian////support: Mac OS X, gnu/linux, Unix, Windows XP////Free to use///////////////////////////////////////////////////////////Importjava.io.File;/** * @authorDem Pilafian *@authorJohn Kristian*/Importjava.lang.reflect.Method;Importjava.util.ArrayList;Importjava.util.List; public classBarebonesbrowserlaunch { public Static voidmain (string[] Args) {String path= System.getproperty ("app.home"); if(path==NULL|| "". Equals (path) {path= System.getproperty ("file.path"); if(path==NULL|| "". Equals (path)Throw NewRuntimeException ("app.home and File.path not configured"); } List<String> URLs =NewArraylist<string>(); if(NewFile (path). isdirectory ()) {File dir=NewFile (path);  for(File file:dir.listFiles ()) {urls.addall (javafile.geturl (file.getabsolutepath ())); }        }Else{urls.addall (javafile.geturl (path)); }                 for(String Url:urls) {openURL (url); }            }         public Static voidOpenURL (String Url) {Try{browse (url); } Catch(Exception e) {}} @SuppressWarnings ({"rawtypes", "unchecked" })    Private Static voidBrowse (String Url)throwsException {//get the name of the operating systemString osname = System.getproperty ("os.name", "" "); if(osname.startswith ("Mac OS")) {            //How to open an appleClass filemgr = Class.forName ("com.apple.eio.FileManager"); Method OpenURL= Filemgr.getdeclaredmethod ("openURL",Newclass[] {String.class }); Openurl.invoke (NULL,Newobject[] {url}); } Else if(osname.startswith ("Windows")) {           //how Windows is Opened.             /*String Browspath = System.getproperty ("brows.path");                Try {if (browspath! = null) {runtime.getruntime (). exec ("browspath" + url); }//runtime.getruntime (). exec ("c:\\program Files (x86) \\Google\\Chrome\\Application\\chrome.exe" + URL            );            } catch (Exception e) {runtime.getruntime (). exec ("rundll32 url.dll,fileprotocolhandler" + url); }            */runtime.getruntime (). EXEC ("rundll32 url.dll,fileprotocolhandler" +url); } Else {            //How to open Unix or Linuxstring[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String Browser=NULL;  for(intCount = 0; Count < browsers.length && Browser = =NULL; count++)                //executes the code, jumps out after the Brower has a value,//here is if the process is created successfully, ==0 is the normal end.                 if(runtime.getruntime (). EXEC (Newstring[] {"which", browsers[count]}). waitFor () = = 0) Browser=browsers[count]; if(browser = =NULL)                Throw NewException ("Could not find web browser"); Else                //This value has been successfully obtained in the above process. Runtime.getruntime (). EXEC (Newstring[] {browser, url}); }    }}

This is the main class, suitable for Mac and Linux,mac system, very powerful, which mainly use Windows system, Open the default browser;

3. Play into the executable jar package: Note the path to the main method,

From Figure 11, next, set the jar package path, then proceed to set figure 2, finally finish;

4. Run the jar through the bat file:

Run.bat file: When let premise is set up Javahome and Classpath etc;

Set dir=%cd%-dapp.home=%cd%\config-jar%cd%\auto.jar

Create a new config folder on the path where the jar is located, so the configuration file is placed in this folder and click Run.bat Test Run Results.

Java program opens the specified address page

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.