0

I have a small runnable JAR application that creates a file.

It works from eclipse, and does not work from JAR.

OutputStream os=new FileOutputStream("C:\\temp\\props.xml"); 
TransformerFactory tf=TransformerFactory.newInstance();
Transformer trans=tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));

There are no errors, nothing breaks, user message is shown like file is created, but there is no file.

Similar topic exists: link, but there is no solution for my problem.

It's probably some file/folder win permission issue.

Is there a way I could allow writing to a file from JAR?

I tried making a java.policy file:

grant
{
 permission java.io.FilePermission "C:\\xxx", "write";
};

I placed that policy file in the same folder where my JAR is, but it didn't helped.

Edit: Reading was the problem. I placed some file in my project and tried to read it like:

File file=new File("someFile.txt"); 
FileInputStream file_inputstream=new FileInputStream(file);

It was ok for eclipse, but not ok for JAR. When I placed this file outside JAR and read it with apsolute file path it worked.

Community
  • 1
  • 1
Brs
  • 1
  • 2
  • 2
    If it works from Eclipse, it's unlikely that it is a file/folder permission issue, unless the path where the file is being attempted to be written is different when you run from Eclipse and from the JAR. Try to use `File.getAbsolutePath()` on the `File` instance after your code created it, to show the full path. It's possible that the file is being created in both cases but not where you expect it. BTW if you don't show the code it's hard for people to tell what's wrong. – SantiBailors Sep 24 '15 at 10:01
  • It would be quite helpful if you show the code which actually creates the file? – SubOptimal Sep 24 '15 at 10:35
  • You might want to run Process Explorer by sysinternals while writing to the file. This monitors the filesystem access and you can get an idea of what is going wrong. And even if the policy file were the right way to do this I don't think Java looks for it in the directory you put it. – Marged Sep 24 '15 at 10:37
  • Do you specify absolute or relative path? Maybe the file is just creates at a location you do not expect... – Matthias J. Sax Sep 24 '15 at 10:40
  • 1
    Are you reading from the same source? You don't show that in your code snippet. – Chris Gerken Sep 24 '15 at 11:04
  • Tnx 2 all! Reading was the problem. I placed some file in my project and tried to read like: File file=new File("someFile.txt"); FileInputStream file_inputstream=new FileInputStream(file); It was ok for eclipse, not ok for JAR. When I placed this file outside JAR and read it with apsolute file path it worked. – Brs Sep 24 '15 at 11:28

0 Answers0