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.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
Damiii
  • 1,363
  • 4
  • 25
  • 46
  • "I know that we can call system calls" - and have you tried running a jar file? If not, why not? – f1sh Mar 18 '16 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. – Harmelodic Mar 18 '16 at 15:04
  • I added more information on my topic, thank you for the answers. :) – Damiii Mar 18 '16 at 15:10

2 Answers2

3

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

Yev
  • 321
  • 3
  • 9
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

Awnen
  • 41
  • 7
  • 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 ! – Damiii Mar 18 '16 at 21:02
  • 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... – Damiii Mar 18 '16 at 21:07
  • what is the exception? share the stack, it works for me and write the output to my output.txt file – Awnen Mar 18 '16 at 22:01
  • `Exception in thread "main" java.lang.ExceptionInInitializerError at mars.MarsLaunch.(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` – Damiii Mar 19 '16 at 03:38
  • 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 – Awnen Mar 19 '16 at 11:16
  • Well i have an empty file :( with the Fibonacci.asm file ! And the result shouldn't be empty ! Can you show me your project ? – Damiii Mar 19 '16 at 13:03