Whereis.exe in Java
Ξ July 6th, 2008 | → 0 Comments | ∇ Coding, Microsoft, Programming, Software, Technical |
Following on from my Where.exe post, I thought I’d knock up another version; in Java this time.
This was actually done using Visual J++ … a simply wonderful product in my opinion. Pity it went the way of the fairies [thanks Sun!].
- As this was done in VJ++, I couldn’t use the String split() method [I believe that appeared in a later JDK]. However I reckon the tokeniser does a better job for me here; and is the right choice imho.
- I also had some trouble finding a way to read my PATH environment variable. Seems like this ability is not built into Java. Still, GetEnvironmentVariable came to the rescue.
- I also used just static methods throughout – that saved me one line of code in not creating an instance!
- One other thing. When it first ran, it threw exceptions [WinIOException]- which took a little debugging. After some head scratching, I realised it was due to my path containing a sub-path that pointed to a non-existent folder.
import com.ms.wfc.io.*;
import com.ms.win32.Kernel32;
import java.util.*;
public class whereis
{
public static void main (String[] args)
{
findIt(args[0]);
}
static public void findIt(String arg)
{
StringBuffer strBuf = new StringBuffer(2000);
int ret = com.ms.win32.Kernel32.GetEnvironmentVariable("Path", strBuf, strBuf.capacity());
StringTokenizer st = new StringTokenizer(strBuf.toString(), ";");
while(st.hasMoreTokens())
{
whereisWorker(st.nextToken(), arg);
}
}
static private void whereisWorker(String path, String file)
{
try
{
FileEnumerator e = new FileEnumerator(File.combine(path, file));
while(e.hasMoreFiles())
{
System.out.println(File.combine(path, e.getName()));
e.getNextFile();
}
}
catch(WinIOException e)
{
// Can get here if a segment of the PATH doesn't actually exist!
}
}
}


