简体   繁体   English

如何运行 jar 文件?

[英]How can I run a jar file?

I searched long to find code to run a jar file, but this is the only code I found:我搜索了很久才找到运行 jar 文件的代码,但这是我找到的唯一代码:

public class Main {
    
    public static void main (String args[]) {
        try {
            Runtime.getRuntime().exec("java -jar C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

but it didn't work.但它没有用。 Just nothing happens.只是什么也没发生。 No Exception, no Error, nothing.没有异常,没有错误,什么都没有。 Can someone help me?有人能帮我吗?

There is at least one cause, because there is no single command with the name至少有一个原因,因为没有一个命令的名称

"java -jar C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar"

What you want, is to execte the command "java" with the 2 arguments "-jar" and "C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar".您想要的是使用 2 arguments“-jar”和“C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar”执行命令“java”。 You can do that by passing the command and its arguments via String array.您可以通过字符串数组传递命令及其 arguments 来做到这一点。 In your case that would be an array of 3 Strings.在您的情况下,这将是一个包含 3 个字符串的数组。

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[]) Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])

In that javadoc you can see that exec returns a new Process object. Such Process provides more information, eg the exit code of your process (via waitFor ) and access to (error) output messages (via getInputStream ).在该 javadoc 中,您可以看到exec返回一个新进程object。此类进程提供更多信息,例如进程的退出代码(通过waitFor )和访问(错误)output 消息(通过getInputStream )。

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Example: Printing a Java InputStream from a Process示例:从进程打印 Java InputStream

Note: You didn't specify which Java version you are using, so i just picked a "random" version for the javadoc links.注意:您没有指定您使用的是哪个 Java 版本,所以我只是为 javadoc 链接选择了一个“随机”版本。 You might need to look for the version you are using.您可能需要查找您正在使用的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM