Executing URI commands in Java

2019-06-24 02:40发布

One way that Steam lets users launch games and perform many other operations, is by using URI protocols, for example (from Valve developer community):

steam://run/<id> will launch the game that corresponds to the specified ID.

steam://validate/<id> will validate the game files of the specified ID.

How can I get Java to 'run' these? I don't even know what you call it, i.e. do you 'run' URIs, or 'execute' them, or what? Because persumably these URIs don't have anything to return, and the URI class in Java doesn't have anything related to 'executing' them, however URL does, but it doesn't work!

I've tried this:

...
try
{
    URI testURI = URI.create("steam://run/240");
    URL testURL = joinURI.toURL();
    // URL testURL = new URL("steam://run/240") doesn't work either
    joinURL.openConnection(); // Doesn't work
    // joinURL.openStream() doesn't work either
}
catch (MalformedURLException e)
{
    System.err.println(e.getMessage());
}
...

Each combination gives the error: unknown protocol: steam.

The system that Steam uses to handle the URIs is definitely working, because for example, I can type the above URI into Firefox and it works.

My eternal gratitude to the person who provides the answer!

Thanks

标签: java uri
3条回答
看我几分像从前
2楼-- · 2019-06-24 03:11

The system that Steam uses to handle the URIs is definitely working, because for example, I can type the above URI into Firefox and it works.

It is working because Firefox (or other browsers) can associate unkown protocols with applications. When you load steam://xxx for the first time, Firefox asks you which application you want to open. If it didn't ask you, steam probably installed a browser plugin for that.

查看更多
放我归山
3楼-- · 2019-06-24 03:19

Try Desktop.browse(URI), this should start the "default action" which is the Steam client for a steam:// URI, e.g.

URI uri = new URI("steam://store/240");
if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().browse(uri);
}
查看更多
Emotional °昔
4楼-- · 2019-06-24 03:23

A Uniform Resource Identifier (URI) just identifies a resource, it doesn't necessarily describe how to access it. Moreover, for custom protocols, such as "steam" the vendor can define any underlying access conventions which compatible client programs must know to interact.

In order to "execute" a URI like this you need to know exactly how the protocol is implemented (is it over HTTP? TCP? UDP?) and how to speak with the server at the other end.

The Valve Developer Community wiki page might have some useful information.

查看更多
登录 后发表回答