Ready to Program Java: opening up a URL in a program


#1

Hi, I am programming with Java (to be specific Ready to Program) and I was wondering how to open up a link in a browser using user inputs. For example, if the user types in "everybody dance now", the program opens up a music video on the default browser. Thank you very much.


#2

This is an awesome question that I would like to know the answer to now that you mention it, I wish I knew, but I don't.


#3

You gotta get used to googling as a programmer
https://www.google.com/search?q=java+open+default+browser


#4

Thank you for your help.
But unfortunately, I have already tried that and the common trend of importing java.awt.Desktop doesn't work with my program. It brings up an error where it says "The import "java.awt.Desktop" is not valid, since it does not name a type in a package.
If you understand what is wrong (whether it is me or the program) and could explain to me, it would be very appreciated.


#5

Then you should be asking about the thing that's stopping you, motivating why you need to ask, showing that you tried, asking the thing really stopping you is way more relevant. All kinds of better.

Did you google that error? If not, keep going until actually stuck. That error message is just another thing to learn to deal with, not something to be skipped by asking for what's at the end of the road. That other person is going to be jumping through the same hoops to get there. What'll happen next time you run into the same error message?

It's pointless to learn how to do everything correctly, you'll gimp yourself by not being able to figure out how to do new things, you would always have to ask

And if you're good at searching for information then you're also going to be a whole lot better at asking for it because you know what information you need to present in your question


#6

Your import, seems to me like you're either just writing the import incorrectly, or that there's something with your environment preventing it from running. Sharing a minimal piece of code that produces that error message would let others find what was written incorrectly, or if they can successfully run it then you gain the information that it's got to do with your environment which would aid in finding a solution


#7

import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.net.URI;
import java.awt.Desktop;

import hsa.Console;

class Test

{
public static void easterEgg1 (Console c)
{
try
{
if (Desktop.isDesktopSupported ())
{
Desktop.getDesktop ().browse (new URI ("https://www.youtube.com/watch?v=PGNiXGX2nLU"));
}

    }
    catch (Exception e)
    {
        e.printStackTrace ();
    }

} // of easterEgg1.


//////////////////
// MAIN PROGRAM //
//////////////////

public static void main (String[] args)

{
    // Sets the screen
    Console c = new Console (30, 100, "The Test");

    easterEgg1 (c);
    
}

}


#8

This is a test to simulate my environment of coding. Sorry I didn't post this sooner.This method is what I kept seeing online so I am wondering why it isn't working. I have tried googling the exact error but it doesn't provide relevant information. I heard that this forum is great for answering questions about programming so I researched for a while prior to asking for help. I didn't receive anything that complies with inquiry.
I hope that if this is answered, it will help others understand this situation.


#9

After removing all use of hsa.Console (that's not part of Java so I don't have it), that compiles and runs.
My guess at this point is that it isn't being compiled correctly, or that different code is used
I'm using java 1.8 and compiling it with the command: javac Test.java
Java 1.6 and 1.7 should also be ok


#11