2

I have code like this:

return (new File(pathA + File.separator + pathB + File.separator)).mkdir();

I believe it used to work, but it does not work now. The application only runs on Linux and we have tested it with Mint 9, Ubuntu 10.04, Kubuntu 12.04, etc. It doesn't create the intended directory.

The path is inside a directory with 777 permissions.

The stack track is not very helpful. Because the trace shows a couple calls to java.security methods, I assume it is a permissions problem. But the actual error message is not helpful at all (probably because the exception handling in the code needs to be improved).

What is the best way to create directories in Java on Linux?

5
  • 6
    Please share the actual error messages. Maybe we do find them more helpful than your vague description. Does new File(path).mkdir() work maybe? The extra separator should never be necessary. Commented May 18, 2012 at 21:11
  • "But the actual error message is not helpful at all (probably because the exception handling in the code needs to be improved)." So improve it and copy/paste the output. Commented May 18, 2012 at 21:12
  • 1
    Not even csi works without evidence. Improve logs and bring them, please Commented May 18, 2012 at 21:13
  • also explain what kind of application it is. is it a web app running inside a servlet container like tomcat, an applet, simple java app, ...? Commented May 18, 2012 at 21:16
  • Seeing the value of "path" where this is failing would also be useful. Commented May 18, 2012 at 21:17

2 Answers 2

3
  • Extract path + File.separator to a variable.
  • Print it out to the console
  • Start a shell in a terminal and run this command using the same user account that the app is running as mkdir <INSERT THE PATH TO THE DIR THAT YOUR APP IS TRYING TO CREATE HERE>

That should give you the root cause of your problem.

If that does not make the answer obvious add all other evidence to the question.

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

1 Comment

You are correct that I left out something important in the question. I inadvertently left out the fact that there were multiple directory levels. I fixed that and also answered with the solution.
2

The problem was that in the path I was creating more than one level of directories. The solution was:

return (new File(pathA + File.separator + pathB + File.separator)).mkdirs();

Notice the "s" on mkdirs().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.