• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Running java program from another java program

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is due to space between "Program" and "Files" in the "Program Files" folder.

change



to

 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But this is printing it as below -

these double quotes shouldn't be there.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Garg wrote:

these double quotes shouldn't be there.



Why not? CMD/C is not complaining about it.
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comment out this portion of code and give it a try:

 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you call "cmd /C" at all?
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It worked.. Thanks a lot!!!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the information
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic