File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes creating directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating directory" Watch "creating directory" New topic
Author

creating directory

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
im just wondering...

how to create a directory within the same location of the .jar file.

im using netbeans 6.8 and the jar file is located on a folder called 'dist'

i wanted my program to create a new folder in that directory, or where ever location the jar file will be moved, when it is run, it will create the directory at the same place




i found this code, but i dont know how to make it do what i wanted,


any ideas will be appreciated. thanks.
Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
Hi Jhedzkie,
According to the code you have posted the important part is:


This will create a directory named "test" in the location that the JAR is ran from.

I think netbeans runs the jar from the netbeans Projects root folder. rather than under "dist", i.e: one level up from the dist directory.

Regards,
TY.
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
hi thanks for the reply.

i did even tried to add an 'else' statement




and it always says that it is error. i wonder what's wrong with the code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Thomas Young wrote:This will create a directory named "test" in the location that the JAR is ran from.

True, but keep in mind that this is not necessarily the location where the JAR is located. With -jar or -cp / -classpath you can run classes from your JAR file from basically anywhere.

Jhedzkie, what does "new File(strDirectory).getAbsolutePath()" return? It might not be what you expect.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
In addition to Rob's question:
Jhedzkie, what does "new File(strDirectory).getAbsolutePath()" return? It might not be what you expect.


What is the error?

Regards,
TY.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

File.mkdir and File.mkdirs, along with File.delete and File.renameTo, return boolean to indicate success rather than throw an exception. This can be annoying since it doesn't tell you the reason why the operation failed. At least with the upcoming java.nio.file.Path class that has been solved, with the following replacements:
File => File.toPath
File.delete => Path.delete
File.renameTo => Path.moveTo
File.mkdir => Path.createDirectory
File.mkdirs => java.nio.file.Files.createDirectories
Tracy Tse
Greenhorn

Joined: May 24, 2010
Posts: 22
Jhedzkie Skies wrote:im just wondering...

how to create a directory within the same location of the .jar file.

im using netbeans 6.8 and the jar file is located on a folder called 'dist'

i wanted my program to create a new folder in that directory, or where ever location the jar file will be moved, when it is run, it will create the directory at the same place




i found this code, but i dont know how to make it do what i wanted,


any ideas will be appreciated. thanks.

i found out that this statement should be changed
Hope this helps!


Whatever it takes is what i have got !
Lift is the best sitcom!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Nonsense. / has no special meaning in Java strings. \ does so yes, \ should be escaped, but not /.
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
i seem to have seen my problem. just a careless mistake. sorry for the bother.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Jhedzkie Skies wrote:. . . sorry for the bother.
Not at all.

Please tell us what the solution was, so future readers can find it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Indeed. I'm now quite curious.
 
wood burning stoves
 
subject: creating directory
 
Similar Threads
Problem with creating a file in the current directory and with the current time stamp
regarding file creation on server -very urgent
File I/O
questions about several File methods.
help needed with creating folder