1

Dear community members,

I have a small problem with the following code. I think it should open the explorer in the C:\Users\Me\AppData\Local\Temp\ directory. However that does not work, actually nothing happens. No errors.

I have used the following code:

import java.awt.Desktop; import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) { try { Desktop.getDesktop().open(File.createTempFile("abcd", ".temp").getParentFile()); } catch (IOException e) { e.printStackTrace(); } } } 

If I replace it with a normal file, like new File("C:\"), then it does work. Can someone explain to me why it does not work?

PS: guys I forgot to tell you I also tried it with some characters like "abcd", it still gives nothing and shows nothing!

3
  • Please specify what "doesn't work" in the post and/or title. The first step to fixing a problem is to identify what the problem is (after, perhaps, identifying that there is a problem ;-) Commented Mar 24, 2012 at 22:22
  • Well, nothing happens. Normally Desktop.open(new File("C:\\")); would open Windows Explorer at the directory specified in the File. Now nothing happens, no error, nothing. It just terminates after executing the code. PS: you can execute the code on a Windows 7 machine and then nothing will happen, while I expect it to open "C:\Users\John Doe\Local\AppData\..." in Windows Explorer Commented Mar 25, 2012 at 9:28
  • Well, what does File.createTempFile(...).getParentFile() evaluate to? (That is, would it "work" if using that value, as a literal. If it is not expected, then that is where to look. If it is as expected, but doesn't open, then that is where to look. Isolate the problem using an iterative process.) Commented Mar 25, 2012 at 21:59

3 Answers 3

5

Just use new File(System.getProperty("java.io.tmpdir")): that's the temp directory. No need for dirty tricks with the parent of a useless temporary file...

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I would like to open the directory in explorer, but I can't seem to do it. Maybe I need to run it as an administrator or something? Even though the account I am using is an administrator account?
Your problem is not in the File API part. You have to figure out what's wrong with the AWT part. Your code snippet is legit, since it works indeed with another directory (and it works on my machine too :]). Try to first ensure your current user has the right to read the temp dir, that this directory exists...
0

Looking at the Javadoc for the File class:

Parameters:

prefix - The prefix string to be used in generating the file's name; must be at least three characters long

So it appears that "" isn't a valid argument for the file prefix.

1 Comment

I tried already using "abcd" it still doesn't fix the problem. Nothing happens :(.
0

According to the docs for File.createTempFile(), if the prefix (first argument) contains fewer than three characters, an IllegalArgumentException will be thrown. You should see it in your console output.

1 Comment

I tried using "abcd" and it still did not show anything. Not even an error and that's what's been buggin me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.