Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to find executable from path  RSS feed

 
Chris Loschen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

My application allows the user to specify an executable to be launched on request. This is working ok when the user specifies the complete path to the executable, but I now have an additional requirement: if the complete path is not specified, find the executable in the system PATH.

I'm not finding anything in the APIs that lets me do that, though it seems like it should be a pretty straightforward thing to do: any suggestions?

My current code finds the file on the client system and if it doesn't already exist, tries to download it automatically, like so:

But if the executable file is on the path rather than in the absolute directory, my code isn't going to find it and I'll try to keep downloading it. I think I may need a different approach, but I'm not sure what.

Thanks for your help!

Chris

JCE: please use code tags
[ May 12, 2008: Message edited by: Joe Ess ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Note that System.getenv() was added in Java 1.5, so the above will not work for Java 1.4 or earlier.
[ May 12, 2008: Message edited by: Mark Vedder ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. on some system's, the PATH env variable may be lowercase (path).

per the System.getEnv() Javadoc:

On UNIX systems the alphabetic case of name is typically significant, while on Microsoft Windows systems it is typically not. For example, the expression System.getenv("FOO").equals(System.getenv("foo")) is likely to be true on Microsoft Windows.


So you may want to check if getEnv() returns null for "PATH", and if so try 'path'. I think some shells on unix use a lowercase 'path' env variable
[ May 12, 2008: Message edited by: Mark Vedder ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Vedder:
Note that System.getenv() was added in Java 1.5, so the above will not work for Java 1.4 or Java 6


Are you sure it won't work on Java 6 ? I haven't tried it so you may be right, but the Javadoc actually uses PATH as an example, so it would be a bit strange if it didn't work.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:


Are you sure it won't work on Java 6 ? I haven't tried it so you may be right, but the Javadoc actually uses PATH as an example, so it would be a bit strange if it didn't work.


D'oh! A small typo. It will of course work in Java 6. I think I meant to say that it won't work for Java 1.4 or 1.3, and for some reason typed Java 6 instead of 1.3. I've corrected it above. Thanks for catching that.
 
Chris Loschen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!

I'm running this in an applet compiled with 1.4, so I altered your code a little bit to remove the Java 5-specific loop, but it's working great.

By the way, I do see System.getenv(String envname) in 1.4, though it's deprecated there but not in 1.5+, and it does seem to be working correctly for me in my applet.

Thanks again!

Chris
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Loschen:
T
By the way, I do see System.getenv(String envname) in 1.4, though it's deprecated there but not in 1.5+, and it does seem to be working correctly for me in my applet.


Interesting. They must have revamped it in 1.5. The only reason I stated getEnv() was not available in 1.4 was that the JavaDoc I was looking at (for Java 6) had a "since 1.5" notation. So I just assumed it was never in 1.4. But now that I look again, I see that the "since 1.5" annotation is on the Map<String,String> getenv() method and not the String getenv(String name) method. Guess I was a little distracted eating my lunch when I answered the question.

Anyhow, glad you are up and running.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!