views:

152

answers:

3

I'm writing a program in scala which call:

Runtime.getRuntime().exec( "svn ..." )

I want to check if "svn" is available from the commandline (ie. it is reachable in the PATH). How can I do this ?

PS: My program is designed to be run on windows

+3  A: 

I'm no scala programmer, but what I would do in any language, is to execute something like 'svn help' just to check the return code (0 or 1) of the exec method... if it fails the svn is not in the path :P

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("svn help");
int exitVal = proc.exitValue();

By convention, the value 0 indicates normal termination.

Miguel Fonseca
I think it would be better to do "which svn" instead of "svn help". It will still give the proper return code as to whether or not svn exists in the path, but on a success you will also get the full-path to the svn executable.
Apreche
+1  A: 

Concerning the original question I'd also check for existence as FMF suggested.

I'd also like to point out that you'll have to handle at least the output of the process, reading available data so the streams won't be filled to the brim. This would cause the process to block, otherwise.

To do this, retrieve the InputStreams of the process using proc.getInputStream() (for System.out) and proc.getErrorStream() (for System.err) and read available data in different threads.

I just tell you because this is a common pitfall and svn will potentially create quite a bit of output so please don't downvote for offtopicness ;)

Huxi
A: 

If you have cygwin installed you could first call "which svn", which will return the absolute path to svn if it's in the executable path, or else "which: no svn in (...)". The call to "which" will return an exitValue of 1 if not found, or 0 if it is found. You can check this error code in the manner FMF details.

Jason Catena