Jump to content

How to open the default browser?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hello!!!
Can someone help me?
I want to open a url in the default browser....But I never found how to do this...Can someone give me a step by step guide?! :crying:

Sorry for the silly question,but I'm really new in java...

Any help is highly appreciated!

Thanks advice!

P.s I need it to work just on windows OS.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Desktop (Java Platform SE 7 )

This class will make life easier for you.
First, make sure the Desktop object is supported by calling the appropriate method from the Desktop class.

After this, call the browse method for your desktop object. As your browse parameter, pass a string of the URL you wish to open.

#3
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hmmm I have made this class:

public class Open {

 

	public static void URL(String url) {

		try {

			Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + url);

		} catch (IOException e) {

		}

	}

 

	public static void main(String[] args) {

		Open.URL("http://google.com");

	}

}


The problem is that the above class can open just 1 url...Is there a way to get a list of urls from a text area,or from a file? - There will be 1 url per line...
Someone who can give me an advice? I'm really new in java...

Thanks for any replay!

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Use for loop to cycle over the list and open them all.
.

#5
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
But how can I read it line by line...I never found the proper commands..I have made the same programm in c++ (without GUI),but in java the way to read a file seems kinda confusing for me.... :(

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The simplest way would be to set up a Scanner object and use the .nextLine() method to consume the file line by line.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users