【问题标题】:Execute Jar file in IDE and get the output (String)? [duplicate]在 IDE 中执行 Jar 文件并获取输出(字符串)? [复制]
【发布时间】:2016-07-05 09:08:34
【问题描述】:

是否可以在我的 IDE (IntelliJ) 上执行一个 Jar 文件以在我拥有的项目中为我自己的目的获取输出字符串?

我知道我们可以进行系统调用,但在这种情况下,我想在我的项目中添加一个 Jar 文件并在需要时执行它。

例如:我在 IntelliJ 上有一个项目,我的一个类(在这个项目上)需要通过运行 Jar 文件(在我的项目上)来获取输出。

在我的终端上,我会执行java -jar <jar_file>.jar <file>.asm 之类的操作,这会将结果输出到我的终端。 我想从我的 Java 类上的这个命令中得到那个输出。

【问题讨论】:

  • “我知道我们可以调用系统调用”——你试过运行 jar 文件吗?如果没有,为什么不呢?
  • to get the output string for my own purpose on the project that I have。请你把这点说得更清楚,并确切地说明你想做什么。
  • 我添加了有关我的主题的更多信息,谢谢您的回答。 :)

标签: java intellij-idea jar


【解决方案1】:

你的 Jar 文件返回一个输出字符串,所以我假设它的主要方法可能如下所示:

public static void main(String[] args) {
    System.out.println("output string");
}

现在,如果你想在你自己的类中使用这个“输出字符串”字符串,你可以这样做:

public class YourClass {
...

    public String getOutputStringFromJar() {
        String s = ""; // or = null;

        try {
            Process p = Runtime.getRuntime().exec("java -jar full/path/to/your/Jar.jar full/path/to/fibonacci.asm");// just like you would do it on your terminal
            p.waitFor();

            InputStream is = p.getInputStream();

            byte b[] = new byte[is.available()];
            is.read(b, 0, b.length); // probably try b.length-1 or -2 to remove "new-line(s)"

            s = new String(b);

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

        return s;
    }
...
}

现在你有了返回输出字符串的方法,你可以随心所欲地使用它,而且你知道如何随时从项目中执行 Jar 文件

【讨论】:

  • 今晚让我试试这个! :)
  • 谢谢!!!它工作得很好,这就是我想要的! :)) 非常感谢!!!
【解决方案2】:

您的问题并不准确,但如果我理解您在做什么,您可以在 param 中运行带有 .asm 文件的 Mars.jar,然后您会得到类似于 this link with Fibonacci numbers 的输出

现在您想在您的程序中获得斐波那契数列吗?如果这是您需要的,我建议您反编译 jar 以了解他的内容

当你这样做的时候,你会看到jar中的主类是这样的

 public class Mars {
   public static void main(String[] args) {
     new mars.MarsLaunch(args);
  }
} 

所以当你将 jar 添加到你的类路径时,你需要做这样的事情

public static void main(String[] args) throws FileNotFoundException {

    // this will redirect your system output to a file named output.txt
    PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
    System.setOut(out);


    String [] myAsmFile = {"C:/produits/Fibonacci.asm"};

    new mars.MarsLaunch(myAsmFile);
    // and then you can read the output.txt file

}

希望对你有帮助

【讨论】:

  • 是的,这就是我想要的!但是,我不知道这是否可行!我看到 MarsLaunch Class 有这个评论! // running from command line. // assure command mode works in headless environment (generates exception if not)我试试看!
  • 我试过并得到了异常(我真的很期待这个)...... MarsLaunch 类有一些代码,如果该类没有在终端上执行,则会输出异常......
  • 有什么异常?共享堆栈,它适用于我并将输出写入我的 output.txt 文件
  • Exception in thread "main" java.lang.ExceptionInInitializerError at mars.MarsLaunch.<init>(MarsLaunch.java:131) at mars.Main.main(Main.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.NullPointerException
  • 它只是一个 nullPointerException,对 jar 没有任何限制,我发布的示例对我来说效果很好,你应该看到你启动的参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2011-09-03
  • 2016-11-03
  • 2013-06-29
  • 2017-10-13
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多