Calling Java program in another Java program

R

Rey

Hi,
I have a Java program which requires it to make a another java
program and use the result it produces. I am working on a very limited
knowledge of java and i don't know how i can achieve it, I will
appreciate any one who can send me an example

Tks
-spirit
 
M

Michael Borgwardt

Rey said:
I have a Java program which requires it to make a another java
program and use the result it produces. I am working on a very limited
knowledge of java and i don't know how i can achieve it, I will
appreciate any one who can send me an example

There are two methods: use java.lang.Runtime.exec() or (preferably) call
the main() method of the other program's main class. However, if the program
works with standard in and out, you need to change System.in and System.out.
 
A

Andrew Thompson

Michael Borgwardt said:
There are two methods: use java.lang.Runtime.exec() or (preferably) call
the main() method of the other program's main class. However, if the program
works with standard in and out, you need to change System.in and
System.out.

I am not sure the OP understands, so I will add..

Java is OO. You do not necessarily need to call
the 'main()' as Michael said, to get data from another
Java package (program or application).

If you can import classes from the other
package and instantiate them, you may never
need to call a main() method of the other
package.

I guess you did not write the other Java program..
Do you have the source of the other program?
Or do you have access to the JavaDocs?
 
T

Tor Iver Wilhelmsen

Michael Borgwardt said:
There are two methods: use java.lang.Runtime.exec() or (preferably) call
the main() method of the other program's main class. However, if the program
works with standard in and out, you need to change System.in and System.out.

Though you will also want to install a SecurityManager to trap any
System.exit() calls from that other app.
 
R

Roedy Green

I have a Java program which requires it to make a another java
program and use the result it produces. I am working on a very limited
knowledge of java and i don't know how i can achieve it, I will
appreciate any one who can send me an example

You can just call the main method of the other program e.g.

Other.main(someparms);

You can use exec. See http://mindprod.com/jgloss/exec.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top