从Java程序启动steam游戏

从Java程序启动steam游戏,java,Java,我正在尝试制作一个java程序来打开我电脑上的文件和应用程序,现在问题只出现在我尝试从这个程序打开steam游戏时,即Planetside 2或Terraria。我尝试过使用Runtime,但也失败了。下面是我试图打开游戏的地方: 公共无效鼠标pressedMouseE事件{ 试一试{ Desktop.getDesktop.browsenew URLpath.toURI; }捕获URISyntaxException | IOException e1{ 试一试{ Desktop.getDeskto

我正在尝试制作一个java程序来打开我电脑上的文件和应用程序,现在问题只出现在我尝试从这个程序打开steam游戏时,即Planetside 2或Terraria。我尝试过使用Runtime,但也失败了。下面是我试图打开游戏的地方:

公共无效鼠标pressedMouseE事件{ 试一试{ Desktop.getDesktop.browsenew URLpath.toURI; }捕获URISyntaxException | IOException e1{ 试一试{ Desktop.getDesktop.opennew文件路径; }捕获IOE2异常{ e2.1.1.1.1.1.2.2.2.1.2.2.2.2.2.2.2.2.2.2.2.2.1.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2; } e1.printStackTrace; } } 如果有人能想办法,我将不胜感激

这是输出错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: steam:\rungameid\218230 doesn't exist.
路径变量为steam://rungameid/218230.

您必须指示Steam.exe进程通过其应用程序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();
    }
}

请注意,第二个代码段是必需的。此外,我正在使用val关键字。

问题中的堆栈后跟踪请查看var路径等于什么,我认为您需要包括整个文件路径。
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();
    }
}