For some reason the file that I input into my GUI isn't being found by Java even though windows explorer finds it just fine.
If you'd like to see a specific part of the code, just ask!

Thanks in advance,
Jack

Recommended Answers

All 17 Replies

Just the tinyest clue about your code would help reduce the number of possible causes to a finite number.

If you are using the standard file open dialog then it's just a bug in your following code. If you are entering the file path/name as a string then it's probably looking in a directory that you don't expect. You can check that out by writing a few bytes to the same file and seeing where it appears in your file system.

As JamesCherill said , you must check the absolute path of the file .

Make sure that the path of the file is correct and it would be more clear if you post the part of code which tries to open/access the file

Member Avatar for hfx642

So...
You just wrote a java program that can't find a file.
Congradulations... I... Guess.
No need for me to see the code.
I believe you!!

Here's the part that I think is failing.

if (strings[1] != stan && new File(files[1]).exists()) {
					resize2(450, 160);
					report.setText("Opening: " + files[1]);
				} else {
					resize2(450, 160);
					report.setText("Button is not assigned file to open.");
				}
				try {
					Runtime.getRuntime().exec("java -jar " + files[1]);
					flag = true;
					launched = true;
				} catch (IOException e1) {

					e1.printStackTrace();
				}
				if (!flag) {
					launch(1);
					launched = true;
					flag = true;
				}
				if (launched)
					wl("Launched 1");
				else {
					wl("Bad Launch 1");
					openHelp(2);
				}
			}

That just launched an application if it can find the file, but the trouble is, it can't find the file and yet it's being found by windows explorer.

A few comment on your condition check...

if (strings[1] != stan && new File(files[1]).exists())

What are "strings[1]" and "stan"? Are they String? If so, you should not use "!=" to compare but equals() method instead (as strings[1].equals(stan)). Also, what is in "files[1]"? Is it a file name or is it a "full path with file name"? If it is just a file name, it may be the source of the problem because Java doesn't know where to look for the file but where it is being called or at its current environment. Therefore, your file current location may be incorrect.

strings[1] is String.
Stan is final String.

Files[1] is a string containg a path of a file entered by the user.

OK, did a user enters the full path? For example, does he/she enter "myfile.txt" or "C:\myfile.txt" or else? What you need is an absolute path, or Java will look for the file in the same folder location as the Java class. Also, you need to use file separator in order to compose the path used in calling because Windows use "\" while Unix-like use "/" file separator. One sample code that I found is here http://www.mkyong.com/java/how-to-construct-a-file-path-in-java/ and may give you an idea about how to deal with the file path.

The user enters the whole path.

Could you show a sample user input and the error you get? Also, what OS you are using?

An example User input might be: C:\Users\Jack\jarfile.jar Currently, I'm using Windows 7, but it's designed to work on any platform, as long as Java is supported.

Arrest me if im incorrect, but isnt it important that if you are using a windows system, you have to use double backslashes (or double forwardslashes) (or something like that) when specifying a whole path?).

Ahhh.. that might be right. but notice that I am running the user input in a cmd prompt, or terminal... so (i tested), there is no need.

Because you want to use it independently from the platform, you need to compose the file using the file separator. You aren't supposed to try to escape or replace the string. Look at the Properties package (java.util.Properties) for the separator. Then recompose the file full path again to use in new File.

The documentation doesn't mention a Separator in the Properties class.

That doesn't seem to be the problem. Instead, it seems that the problem is that when a user enters a full path, terminal tries to run it from the directory where the .jar file is. What I need is a way to specify the working directory to the root of the computer (Meaning "C:\" in Windows, and "/" in *nix).

What if I used a process builder?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.