• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime() help plzzzzzzzz

 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JiG Intermediate.
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not understand your question.
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To run the java program



The result will be
C:\work\java>java PP
test
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will try this thanx alot for ur help
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what if i need to pass a value to the other program like if :


how can i run it?
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to use the getOutputStream()
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if the arguments are variables??
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if the arguments are variables??


one way is like this:


Rene
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene,,
i try what u say and its work thx alot
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i use this:
??
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll say yes.

Have you tried it?

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

Rene
 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz help me i dont know what to do
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its windows, then you can use this code.

 
nana Amr
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its work
thaxxxxxxxxxx alot
 
Arjun K
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic