Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating directory

 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nonsense. / has no special meaning in Java strings. \ does so yes, \ should be escaped, but not /.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i seem to have seen my problem. just a careless mistake. sorry for the bother.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. I'm now quite curious.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic