• 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

Calling OS commands from within Java

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
Is it possible to call OS (Unix) commands from within Java?
Thanks in advance
Ambrose
 
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 "smart-ass" answer is "Only if your software is running on Unix"
As long as you don't mind losing portability and "pure Java", you can use the various forms of Runtime.exec() to run external software on the host machine.
Note that many commands available at the Unix prompt are actually implemented diredctly by your shell, so you may need to run them using a shell rather than directly. Also note that Runtime.exec() captures the standard input and standard output streams of the executed process, so don't expect diagnostic output to appear on the Java console.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ambrose,
Don't do it! You defeat the purpose of using JAVA (platform independent). What happens if you move your code to Windows? BOOM! You might as well use C/C++.
If you can tell us the reason why you need to do it, we might suggest other work around that is JAVA compliant.
-Peter
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm stuck in a similar position to Ambrose. It is fairly critical that a program be aware of where it is running at, and that is one of the first reasons I would want to query environment variables. Knowing if your in test, or in production is critical.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nate Mattson:
....and that is one of the first reasons I would want to query environment variables. Knowing if your in test, or in production is critical.


Setting system properties when starting a Java program from a command line is easy using the -D option; the program can query by property name:
System.getProperty("thePropertyName");
Referencing the value of an environment variable from a command line (so you can pass it as system property) is possible in Unix and Windows, but the command line syntax varies.
Unix: java -DthePropertyName=$theEnvVariableName
Windows: java -DthePropertyName=%theEnvVariableName%
Maybe this can help...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic