aspose file tools*
The moose likes Java in General and the fly likes Running java program from another java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running java program from another java program" Watch "Running java program from another java program" New topic
Author

Running java program from another java program

Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38
Hi,

For now i am not specifying the java file name -
String javac_exe_file = "C:\\Program Files\\Java\\jdk1.5.0_05\\bin\\javac";
String cmd = "cmd /C" + " " + javac_exe_file;


Runtime rr = Runtime.getRuntime();
Process p = rr.exec(cmd);

i am getting the below error while running the above code -

'C:\Program' is not recognized as an internal or external command,operable program or batch file.

Could somebody please tell me what needs to be done so that it takes C:\\Program Files\\Java\\jdk1.5.0_05\\bin\\javac as an single argument.

Also please can anyone provide me a web link where i can get the useful info related to above concept.

Thanks
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
The error is due to space between "Program" and "Files" in the "Program Files" folder.

change



to

Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38

But this is printing it as below -

these double quotes shouldn't be there.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Manoj Garg wrote:

these double quotes shouldn't be there.


Why not? CMD/C is not complaining about it.
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38

In that case, command will be --- cmd /C "C:\Program Files....\...\javac"
here is my code -

Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Comment out this portion of code and give it a try:

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Why do you call "cmd /C" at all?


http://home.arcor.de/hirnstrom/bewerbung
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38
Then how can we run the javac.exe if we are running the program in windows.
There may be another way but i have seen a lot of examples using the same way as in the current case.
please suggest.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
goole javax.tools.JavaCompiler

By the way the subject line of this thread is "Running java program from another java program". Javac is for compiling .java files to .class files not executing .class files. What exactly are you trying to accomplish?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Yes - invoking the java-class directly, or "X:/path/to/java/bin/javac.exe" without the windowsshell in between. javax.tools.JavaCompiler is of course much more elegant, but might need a little reading of documentation.
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38

My aim is to run a java program from another one.

--In my code there will be one statement similiar to -

As i already specified i am not giving the class name for now..just want to get a flavour how it works..
So is it possible without invoking command shell if we running it in windows
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
My aim is to run a java program from another one.


It is still unclear if you are trying to compile a java file or trying to run a java program. You get better help if you explain your question better. javac compiles .java files, it does not run a java file

Yes compiling a file using javax.tools.JavaCompiler will take some reading, although not much, but if you are merely trying to run a compiled class it does not take much more than calling the main method of the class. You might want to put the call to main in a new thread depending on what you are trying to achieve.
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38
I am trying to complile and then run the program.
I have seen that by using below code we can get the output of it.

Runtime rr = Runtime.getRuntime();
Process p = rr.exec(cmd);

Then we catch the result using Input Streams.. Is my approach not right?
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Manoj Garg wrote:I am trying to complile and then run the program.


And why do you need dynamic compilation?

Manoj Garg wrote:Then we catch the result using Input Streams.. Is my approach not right?


Well if it is anything more than a quick and dirty throw away program, you are creating a maintenance nightmare. Not everyone will be using the same version of JDK, and even in case of same version the folder name or installation path of the JDK could be different. Even if you are the only one running the program, and you will be running it on only one system, you will have to recompile the code when you upgrade to newer version of JDK.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

My hint was just, that you don't need "cmd /C":



- well, of course followed by the javafile to compile, and maybe classpath and other switches used for compiliation.

But there is a java class, which could be used instead - I've never used it before, so I can't guide you in how to use it.

I'm not that pessimistic about the maintainance nightmare. Without further information, that is very speculativ, isn't it?

If the resulting class from the compilation contains a main-Method, you hopefully will invoke it with

, not with another runtime call.
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38


Okey.. It seems i should clear the picture..

Normally if we are working in windows and not using any IDE then we do open a command promt, compile a java program then run it...

suppose exactly samething i want to do it from a GUI just for me.. making a java program then entering the java file name in text field..getting it in my program then compiling and running.. the change is - i want to open a gui by running it from command prompt just once.. after that i enter the java file name..there is a compile and run button. the output which we see on command prompt i want to show it in text area..

I hardcoded the dir command and it worked.. but when i hardcoded javac and entered the java file name it didn't work..

There may be a no. of ways of doing it.. but i think this is one of them.

This is just practicing new things and i think its the way how we learn.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Normally if we are working in windows and not using any IDE then we do open a command promt, compile a java program then run it...


I guess I will not bother asking why no IDE or build tool (Ant, Pache Mavin etc) , seems like you have already made up your mind.

I hardcoded the dir command and it worked.. but when i hardcoded javac and entered the java file name it didn't work.. ...


The last updated source you posted actually does work. The only problem is that you are trying to read the standard out of the sub process before reading the standard error. Javac writes its default help message to standard error. Since there is nothing to read, stdInput.readLine()call blocks and your code never gets to stdError.readLine() and you see no output. That is why I asked you to run the program with the first readline part commented out. For your actual gui program you would want to put the two readline loops in different threads and likely display the output in two different textareas.
Manoj Garg
Ranch Hand

Joined: Jun 21, 2008
Posts: 38

It worked.. Thanks a lot!!!
pourush rohila
Greenhorn

Joined: May 04, 2012
Posts: 11
thanks for the information
 
Don't get me started about those stupid light bulbs.
 
subject: Running java program from another java program