aspose file tools
The moose likes Java in General and the fly likes Compilation of java code at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Compilation of java code at runtime" Watch "Compilation of java code at runtime" New topic
Author

Compilation of java code at runtime

Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
My program is trying to compile all the .java file under a defined directory.
This code works well on Window 7 but when same code is get executed on Liunx, it show an error "cannot read : *.java".
I have already tried a lot of option, but nothing working on linux machine.. I am literally piced-off ..
Can someone help me out , what is going wrong here.
Here is code.



Any help will be appreciated..
Lanny Gilbert
Ranch Hand

Joined: Jun 11, 2002
Posts: 103
A couple of things come to mind.

1.) Your program has the classpath and directory set to "."
Is it possible that the directory passed into your program is the same as "." on Windows, but not on Linux? By thing, I mean are you running your program
in the same directory as the java files you want to compile on Windows, but not on Linux?
2.) Does the directory name passed into your program contain spaces?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

You have fallen for one of the traps detail in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . The '*' in "*.java" needs to be interpreted by a shell so you need.


Note the way I have made the 'javac' command all one argument. On *nix that matters, on windows it does not matter as much but there are other problems.

P.S. You should read that article because you have also failed to handle the Process 'stdout' which could cause a deadlock. Since you don't really care about either 'stdout' or 'stder'' you would do better to use ProcessBuilder then you can merge 'stdout' and 'stderr' and just print out one stream.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Also look at the JavaCompiler class. It allows you to invoke the compiler without Runtime.exec()


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And welcome to the JavaRanch.
Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
Lanny Gilbert wrote:A couple of things come to mind.

1.) Your program has the classpath and directory set to "."
Is it possible that the directory passed into your program is the same as "." on Windows, but not on Linux? By thing, I mean are you running your program
in the same directory as the java files you want to compile on Windows, but not on Linux?
2.) Does the directory name passed into your program contain spaces?


1. By Runtime class, i am trying to invoke the JAVAC command under source code directory .. And it is same on window ans well as linux. (As least Runtime.exec() documentation says that).
2 . Directory name doesn't contain any space.

Thanks for your time to look into this..
Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
James Sabre wrote:You have fallen for one of the traps detail in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . The '*' in "*.java" needs to be interpreted by a shell so you need.


Note the way I have made the 'javac' command all one argument. On *nix that matters, on windows it does not matter as much but there are other problems.

P.S. You should read that article because you have also failed to handle the Process 'stdout' which could cause a deadlock. Since you don't really care about either 'stdout' or 'stder'' you would do better to use ProcessBuilder then you can merge 'stdout' and 'stderr' and just print out one stream.



I even tried to give the full path of java source class, but it was giving me same problem..
Still i would like to try your option..
Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
James Sabre wrote:You have fallen for one of the traps detail in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . The '*' in "*.java" needs to be interpreted by a shell so you need.


Note the way I have made the 'javac' command all one argument. On *nix that matters, on windows it does not matter as much but there are other problems.

P.S. You should read that article because you have also failed to handle the Process 'stdout' which could cause a deadlock. Since you don't really care about either 'stdout' or 'stder'' you would do better to use ProcessBuilder then you can merge 'stdout' and 'stderr' and just print out one stream.



i just tried this.. and it doesn't worked out ..
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Parmendra Tyagi wrote:
James Sabre wrote:You have fallen for one of the traps detail in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . The '*' in "*.java" needs to be interpreted by a shell so you need.


Note the way I have made the 'javac' command all one argument. On *nix that matters, on windows it does not matter as much but there are other problems.

P.S. You should read that article because you have also failed to handle the Process 'stdout' which could cause a deadlock. Since you don't really care about either 'stdout' or 'stder'' you would do better to use ProcessBuilder then you can merge 'stdout' and 'stderr' and just print out one stream.



i just tried this.. and it doesn't worked out ..


Works for me - I tested it before I posted it. I initially tested using Runtime.exec() and then later using ProcessBuilder and both worked flawlessly. What are the symptoms of "it doesn't worked out" ? What does your code now look like? What is the directory structure of '.' ? Are you sure you are specifying the correct working directory? Even though it should make no difference, are you processing 'stdout'.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

James Sabre's solution is the right one. If you tried it, and it didn't work, you are doing it wrong. Note that there are only three arguments: "sh", "-c", and "javac -d . -cp . *.java". It won't work if you break the command line into multiple arguments.


[Jess in Action][AskingGoodQuestions]
Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
James Sabre wrote:
Parmendra Tyagi wrote:
James Sabre wrote:You have fallen for one of the traps detail in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . The '*' in "*.java" needs to be interpreted by a shell so you need.


Note the way I have made the 'javac' command all one argument. On *nix that matters, on windows it does not matter as much but there are other problems.

P.S. You should read that article because you have also failed to handle the Process 'stdout' which could cause a deadlock. Since you don't really care about either 'stdout' or 'stder'' you would do better to use ProcessBuilder then you can merge 'stdout' and 'stderr' and just print out one stream.



i just tried this.. and it doesn't worked out ..


Works for me - I tested it before I posted it. I initially tested using Runtime.exec() and then later using ProcessBuilder and both worked flawlessly. What are the symptoms of "it doesn't worked out" ? What does your code now look like? What is the directory structure of '.' ? Are you sure you are specifying the correct working directory? Even though it should make no difference, are you processing 'stdout'.


I have change the command as you suggest, but facing the different problem - "Unable to launch Java complier. Please check you have install JDK". Please check the first post where i have put the whole method definition..
Thanks for helping out.
Parmendra Tyagi
Greenhorn

Joined: Feb 12, 2010
Posts: 6
Great Thanks to James Sabre and Ernest Friedman-Hill .. I was making a silly mistake. I was using

While the correct one is -
Abstract Meta
Greenhorn

Joined: Apr 12, 2012
Posts: 1
How about using some compilation utility, here is one option

http://code.google.com/p/compilation-toolbox/

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Compilation of java code at runtime
 
Similar Threads
GUI update.
How to call a Perl program in Servlet?
Dumping MySql database
Help for getting output of Runtime.getRuntime().exec()
run dos commands through servlet