• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Compilation of java code at runtime

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also look at the JavaCompiler class. It allows you to invoke the compiler without Runtime.exec()
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the JavaRanch.
 
Parmendra Tyagi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Parmendra Tyagi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Thanks to James Sabre and Ernest Friedman-Hill .. I was making a silly mistake. I was using

While the correct one is -
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using some compilation utility, here is one option

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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic