• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Calling OS commands from within Java  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Is it possible to call OS (Unix) commands from within Java?
Thanks in advance
Ambrose
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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...
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!