• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling createFile from within a jar  RSS feed

 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am creating a small utility that stores user information in a flat file. I use


The class file where this code is in, is packaged in a jar file and will be used by a application running on a web server. Just wanted to know if the above code will be able to create a new file ser.ver even if its running from a jar file as i am not given any paths for where the file should be created ?

Kind Regards,
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No reply ? Do i need to make myself more clearer ?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sajjad Dar wrote:No reply ? . . .
Don't know why nobody replied. Sorry. I would have thought that would work, but you will have to try it; please report to us what happened.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files inside JAR and ZIP files are not really files anymore - they are part of a larger file (the JAR / ZIP file). This means you cannot use java.io.File, java.io.FileInputStream or java.io.FileOutputStream.

Another limitation is that you cannot simply change a file inside a JAR / ZIP file. Any program that allows you to do this uses a shadow file - when a modified file is saved, the JAR / ZIP file is recreated, first as a temporary file, when that's done the original file is deleted and the temporary file becomes the original file.

In this case you can use the WEB-INF folder or any sub folder of it; using ServletContext.getRealPath you can get the file location after which you can use java.io.File, java.io.FileInputStream and java.io.FileOutputStream.

Let's say you use sub folder "ser", then the following can be used:
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.

What i was really trying to say that when i say file.createFile(fileName) and the code is running in a jar file, deployed inside a war file running on the server, where exactly would this file be created. I do suppose the jvm would not try to add the file inside the jar file ?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sajjad Dar wrote:Thanks Rob.

What i was really trying to say that when i say file.createFile(fileName) and the code is running in a jar file, deployed inside a war file running on the server, where exactly would this file be created. I do suppose the jvm would not try to add the file inside the jar file ?

It would put it in "the current directory", wherever that is. Probably the root of the (Tomcat) server installation, possibly the server's temp folder, but you shouldn't rely on any location. That's why I suggested getRealPath - that way, you get control over the location. Or, if you want to store it outside the web app folder, specify some other directory; you may be able to base it on the CATALINA_HOME variable in Tomcat.
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. That makes sense.

Kind Regards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!