我正在尝试编写一个java程序来打开我电脑上的文件和应用程序,现在只有当我试图从这个程序打开一个steam游戏(即Planetside 2或Terraria)时才会出现问题。我尝试过使用Runtime,但也失败了。这是我尝试打开游戏的地方:
public void mousePressed(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URL(path).toURI());
} catch (URISyntaxException | IOException e1) {
try {
Desktop.getDesktop().open(new File(path));
} catch (IOException e2) {
e2.printStackTrace();
}
e1.printStackTrace();
}
}如果有人能试着弄清楚这一点,我将不胜感激!
这是输出错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: steam:\rungameid\218230 doesn't exist.路径变量是"steam://rungameid/218230“。
发布于 2018-06-27 19:48:29
您必须指示Steam.exe进程通过其app id打开您的游戏,或者您可以使用steam协议:
import lombok.val;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import static java.awt.Desktop.getDesktop;
import static java.util.Arrays.asList;
public class SteamGameExecutor
{
public static final String STEAM_INSTALLATION_PATH = "C:\\Program Files (x86)\\Steam\\Steam.exe";
private static final boolean USE_STEAM_PROTOCOL = true;
public static void startGameById(String id) throws Exception
{
if (USE_STEAM_PROTOCOL)
{
val desktop = getDesktop();
val steamProtocol = new URI("steam://run/" + id);
desktop.browse(steamProtocol);
} else
{
startProcess("-applaunch", id);
}
}
private static void startProcess(String... arguments) throws IOException
{
val allArguments = new ArrayList<String>();
allArguments.add(STEAM_INSTALLATION_PATH);
val argumentsList = asList(arguments);
allArguments.addAll(argumentsList);
val process = new ProcessBuilder(allArguments);
process.start();
}
}协议版本应该更具可移植性/平台无关性,除非它在Linux/Mac上不存在。
要查找搜索,您可以使用this app id。例如,以下是以编程方式查找应用id的实现:
import lombok.val;
import java.io.IOException;
import static java.net.URLEncoder.encode;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.jsoup.Jsoup.connect;
public class SteamAppIdFinder
{
private static final String SEARCH_URL = "https://steamdb.info/search/?a=app&q=";
private static final String QUERY_SELECTOR = "#table-sortable > tbody > tr > td:nth-child(1) > a";
public static String getAppId(String searchTerm) throws IOException
{
val completeSearchURL = SEARCH_URL + encode(searchTerm, UTF_8.name());
val connection = connect(completeSearchURL);
val document = connection.get();
val selectedElements = document.select(QUERY_SELECTOR);
val anchorElement = selectedElements.get(0);
return anchorElement.text();
}
}https://stackoverflow.com/questions/28652399
复制相似问题