2

Is it possible to execute a Jar file on my IDE (IntelliJ) to get the output string for my own purpose on the project that I have?

I know that we can make system calls, but in this case I want to add a Jar file on my project and execute it whenever I want it.

For example: I have a project on IntelliJ, one of my classes (on this project) needs to get the output by running the Jar file (which is on my project).

On my terminal, I would do something like java -jar <jar_file>.jar <file>.asm and this would output a result to my terminal. And I want to get that output from this command on my Java Class.

3
  • "I know that we can call system calls" - and have you tried running a jar file? If not, why not? Commented Mar 18, 2016 at 14:51
  • to get the output string for my own purpose on the project that I have. Please can you make this clearer and dictate exactly what you want to do. Commented Mar 18, 2016 at 15:04
  • I added more information on my topic, thank you for the answers. :) Commented Mar 18, 2016 at 15:10

2 Answers 2

3
+100

Your Jar file returns an output string, so I assume, it's main-method could look like:

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

And now, if you want to use this "output string" string in your own class, you could do it like this:

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;
    }
...
}

Now you have the method, that returns the output string, and you can use it however you want, and you know how to execute a Jar file from your project whenever you want

Sign up to request clarification or add additional context in comments.

2 Comments

let me try this tonight ! :)
Thank you !!! It works perfectly and that's what I wanted ! :)) Thank you so much !!!
0

Your ask is not precise, but if i understand what you're doing, you run the Mars.jar with .asm file in param, and you got an output like in the this link with Fibonacci numbers

and now you want to get the Fibonacci numbers in your program? if that is what you need, i would suggest you to decompile the jar to understand his content

When you do so, you'll see that the main class in the jar is like this

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

so simply when you add the jar to your class path, you need to do somthing like this

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

}

hope this help you

6 Comments

Yes this is what I want ! But, I do not know if this will work ! I saw that MarsLaunch Class has this comment ! // running from command line. // assure command mode works in headless environment (generates exception if not) I will try it out and see !
I tried and got exception (I was really expecting this sincerely)... The Class MarsLaunch has some code which will output an exception if the class isn't being executed on a terminal...
what is the exception? share the stack, it works for me and write the output to my output.txt file
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
its just a nullPointerException, there is no restriction on the jar, and it works fine for me with the example i post, you should see the parameter you launch with
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.