Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

how to open the url with out using url class in java

can you pls help me
Posted
Updated 21-Feb-20 0:53am

Simple.

Take that URL and Throw it against the Operating System.

the OS will use the default browser to open the link and show it to the user:

Java
try {
  Desktop desktop = java.awt.Desktop.getDesktop();
  URI oURL = new URI("http://www.google.com");
  desktop.browse(oURL);
} catch (Exception e) {
  e.printStackTrace();
}


This is safe, cause it does not have any application reference to any browser or so - the OS decides. It's also a great way to deal with PDF files.
 
Share this answer
 
Comments
hasitha452 13-Jul-13 1:30am    
what is the imports for this programme
TorstenH. 14-Jul-13 13:38pm    
your IDE will decide that for you. It's all basic Java, nothing special. So if there are two different Objects "URL" to choose from - use the simpler one.
Here is a tutorial utilizing the Jakarta Commons[^] library: HTTP Client Tutorial[^].

Regards,

Manfred
 
Share this answer
 
Java
String url_open ="your_url";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open));
 
Share this answer
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class DownloadPage {

public static void main(String[] args) throws IOException {

// Make a URL to the web page
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");

// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();

// Once you have the Input Stream, it's just plain old Java IO stuff.

// For this case, since you are interested in getting plain-text web page
// I'll use a reader and output the text content to System.out.

// For binary content, it's better to directly read the bytes from stream and write
// to the target file.


BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line = null;

// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
 
Share this answer
 
Comments
Pandu Rang 4-Feb-15 7:38am    
Never share/write complete code for OP. try to provide solution / suggestion let the OP write down the code for himself.
freewizard 15-Jun-16 1:08am    
Never paste your code like this, it is unreadable.
Richard MacCutchan 15-Jun-16 3:39am    
Before posting in messages check the date.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900