searching through directory looking for browser

Hello all,

I was wondering if there is any way to search for the location of a file in your Java application and then launch the .exe file that is at that location.

(example: look for where Iexplore.exe is located on the current machine and then open up Internet Explorer using the the Runtime and Process classes.)

The way I have it now is a hardcoded path to Iexplore.exe. If it is not at that location, it will fail and I will display message "File not found at that location."

Thanks for all your help ahead of time,

jmschrei

[573 byte] By [jmschrei] at [2007-9-26 2:07:31]
# 1

You can launch explorer/navigator by

Runtime.getRuntime().exec("start http://www.web.com");

About searching a file:

public void searchFile(String dirName, String file)

throws IOException {

File directory = new File(dirName);

String path;

if (directory.exists() == true) {

path = directory.getPath();

if (directory.isDirectory() == true) {

File [] fileList = directory.listFiles();

for (int i = 0; i < fileList.length; i++) {

path = fileList.getPath();

if (fileList.isDirectory())

searchFile (path, file);

else if (fileList.isFile()) {

if(fileList.getName().equals(file))

Runtime.getRuntime().exec(fileList.getPath());

}

}

}

else {

if(fileList.getName().equals(file))

Runtime.getRuntime().exec(fileList.getPath());

}

}

}

}

This method should be called like:

searchFile("C:\", "iexplore.exe");

I hope it helps you.

llturro at 2007-6-29 8:54:55 > top of Java-index,Archived Forums,Java Programming...
# 2

Thanks for your help. However I can't get your code example to find the file which I know is on my C: drive.

I made small changes like:

fileList.isDirectory()

instead of

fileList.isDirectory()

to get your version to compile but it won't do it. Is there something I am forgetting. Any help you could give me would be wonderful.

thanks

jmschrei

jmschrei at 2007-6-29 8:54:55 > top of Java-index,Archived Forums,Java Programming...
# 3
I meant fileList instead of fileList
jmschrei at 2007-6-29 8:54:55 > top of Java-index,Archived Forums,Java Programming...
# 4
fileList [ ]
jmschrei at 2007-6-29 8:54:55 > top of Java-index,Archived Forums,Java Programming...
# 5

Well, there is a problem when writing the array element symbol, simply doesn't appear!

Here is the code with the correct use of fileList:

public void searchFile(String dirName, String file)

throws IOException {

File directory = new File(dirName);

String path;

if (directory.exists() == true) {

path = directory.getPath();

if (directory.isDirectory() == true) {

File [] fileList = directory.listFiles();

for (int i = 0; i < fileList.length; i++) {

path = fileList[ i ].getPath();

if (fileList[ i ].isDirectory())

searchFile (path, file);

else if (fileList[ i ].isFile()) {

if(fileList[ i ].getName().equals(file))

Runtime.getRuntime().exec(fileList[ i ].getPath());

}

}

}

else {

if(fileList[ i ].getName().equals(file))

Runtime.getRuntime().exec(fileList[ i ].getPath());

}

}

}

llturro at 2007-6-29 8:54:55 > top of Java-index,Archived Forums,Java Programming...