• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime exec() - '*' flag allowed?`

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm trying to use the Runtime class to do some compiling. The following code works fine under a windows environment but not under linux. Any suggestions?
public static void main( String args[] ){
Runtime r;
r = Runtime.getRuntime();
try{
r.exec("javac /home/bart/vf/*.java");
}catch (Exception e){System.out.println(e);};
}

Thanks
BArt
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the way i could implement r.exec(javac/*.java); is
r.exec("javac[space]*.java"); written in the class RunTimeTry.java in c:\1.2\test and compiling the RunTimeTry.java at c:\1.2\test. the code is compiling well.
I would suggest that you try the similar compiling command in
r.exec("");as you use at the command prompt in linux.
the code should run well.
mind it exec() is platform dependent.
better luck this time.
[This message has been edited by abj (edited June 06, 2000).]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem here is with the difference in the way command lines work on Windows (DOS) and Unix. Unix has traditionally had a command line interpreter (known as a "shell") which is clever enough to expand wildcards and regular expressions on its own. DOS, on the other hand, has a very dumb command line interpreter which passes exactly what is typed to the named program. This means that programs written for DOS and Unix make different assumptions about the arguments supplied to them.
If you type "java *.java" into a Unix shell, the shell expands the arguments and passes a command line of (for example) "java A.java B.java C.java" to the compiler. So the compiler just has to loop through the named files anc comopile them.
If you type "java *.java" into a DOS prompt, it does no processing and passes a command line of "java *.java" to the compiler. So the compiler has to search the current directory for files which match the pattern.
This is all fine if you are actually using a command line interpreter, but if you use Runtime.exec(), you are not. Runtime.exec() invokes the program directly, bypassing the CLI.
On DOS, because programs have to process their own wildcards, this works. On Unix, because programs assume the shell has done it for them, it doesn't; there is no file "*.java"!
The work around is simple; On Unix, use a shell to expand the wildcards for you:
Runtime.exec("sh -c 'javac *.java');
For a really portable solution though, you will need to build up the full list of files yourself, and always call external programs with a list of real filenames, never wildcards.
 
reply
    Bookmark Topic Watch Topic
  • New Topic