• 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

echo <Environment Variable>

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I want to run the command using java code

echo $PATH

I used Runtime.getRuntime().exec() for this, but it returns outputp as : $PATH.

I know I can get the value of PATH variable using System.getProperty() but I want to use particularly "echo" command.

How can I do it?
 
Ranch Hand
Posts: 55
Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can try this it worked for me in windows environment:

May be for Unix environment you might have to use "sh" instead of "cmd.exe".
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

$PATH is resolved by the shell, so you'll need to execute this command using a shell: sh -c $PATH. If you're using Windows, the command should be cmd /C echo %PATH%. Note that Windows uses %PATH% instead of $PATH.

Edit: basically what N Sahni said
There is just one flaw in that code: the error stream should also be read. See When Runtime.exec() won't. In this case the solution is simple: add process.redirectErrorStream(true); before line 7.
 
SachinP Kale
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that worked.

But, one question.

As you are redirecting errorStream here, it means it shows as error if we run "sh -c $PATH", right?

Also,
when I run "echo \$PATH" on shell, it gives me output as: $PATH.
Doesn't it mean that Java automatically adds "\" before "$" sign as Runtime.getRuntime().exec("echo $PATH") also gives me the same output?

Thanks for the help.
reply
    Bookmark Topic Watch Topic
  • New Topic