wood burning stoves 2.0*
The moose likes Java in General and the fly likes Runtime.getRuntime() help plzzzzzzzz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.getRuntime() help plzzzzzzzz" Watch "Runtime.getRuntime() help plzzzzzzzz" New topic
Author

Runtime.getRuntime() help plzzzzzzzz

nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
hi all,,
im working at a project which must: take student homework then check if the homework is correct or not
ex: student homework saved on a file name "hwfile.java" so my program will
compile and then run this file and get the output,, then compair this output with the correct answer(correct answer entered by techer and saved on text file...)
my problem is how to use Runtime.getRuntime().exec(...) to combile and run a file???
what is the parameter will be " Runtime.getRuntime().exec(???) " ??
HELP
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
The parameter to the exec should be the command that has to be executed, in your case you must give "javac to compile and java to execute".

Process p = Runtime.getRuntime().exec("javac Test.java");

Then you can use any one of these methods of process class, depends of your need, getInputStream will return the result of the executed process.

public abstract java.io.InputStream getErrorStream();
public abstract java.io.InputStream getInputStream();
public abstract java.io.OutputStream getOutputStream();
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to JiG Intermediate.


[Jess in Action][AskingGoodQuestions]
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
Sanju Thomas
thax alot for ur reply,,,
but plz tell me should i determain the bath where the javac is?? like if we trying to combile or run at cmd?

and if the file.cpp what is the parameter be??
plz answer me and thx alot again
[ May 18, 2005: Message edited by: nana mm ]
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
I did not understand your question.
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
i try what u told me, its compile the file put i dont know how to get the output using the methods u write

and if the file is a c++ file (file.cpp) what the parametr should be in
.exec(?? file.cpp) if u know tell me plz
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
can you plz explain me how to use this:
public abstract java.io.InputStream getErrorStream();
public abstract java.io.InputStream getInputStream();
public abstract java.io.OutputStream getOutputStream();
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Suppose that you need to compile this program



This program have an error, you must have seen println is not closed.

Now how we can compile this program and get the errorStream ?



See the output

C:\work\java>java PP
Test.java:6: ')' expected
System.out.println("test";
^
1 error

In this way you can get the ErrorStream, InputStream and OutputStream.
Sorry for the late replay, I was bit busy.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
To compile c++ or any other program you need to use the required compiler. I think gcc is used to compile c++ program.

Runtime.getRuntime().exec("gcc test.cpp");

gcc must be available in the system path.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
To run the java program



The result will be
C:\work\java>java PP
test
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
i will try this thanx alot for ur help
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
but what if i need to pass a value to the other program like if :


how can i run it?
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Then you need to use the getOutputStream()
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
im sorry i asked to much but
can i pass the arguments in someway like:
Process pr=Runtime.getRuntime().exec("java test 10 20");
and the a=args[0]; b= args[1]?? or it done in diffrent way
[ May 19, 2005: Message edited by: nana mm ]
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
and if the arguments are variables??
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

and if the arguments are variables??


one way is like this:


Rene


Regards, Rene Larsen
Dropbox Invite
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
Rene,,
i try what u say and its work thx alot
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
i need to execute a file which is in a diffrent path i try this:

but it dose not work
then i try this:

and not work too??
plz tell me what to do
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What are the errors you are getting?

If you type 'javac' in a Command prompt you'll see the options you can set on 'javac'

e.g. javac -sourcepath D:/files/Gproject Test.java

Rene
[ May 22, 2005: Message edited by: Rene Larsen ]
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
can i use this:
??
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I'll say yes.

Have you tried it?

PS.: Just remember that your java class name is with lowercase.

Rene
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
i can run the program coz i dont know how to pass the directory:

but it not work and i dont know what to do??
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
plz help me i dont know what to do
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
If its windows, then you can use this code.

nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
its work
thaxxxxxxxxxx alot
Arjun K
Ranch Hand

Joined: Mar 09, 2005
Posts: 39
If you are working on j2se 5.0 then you can use processbuilder to run a process.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.getRuntime() help plzzzzzzzz
 
Similar Threads
How to run a exe file in java by taking parameters
Retrieving classes of a package
Runtime.exec() not working properly
Online Compiler
spaces in file name