0

I'm trying to change the java working directory, which means, I'm trying to change user.dir property.

For instance, the code below will create a file called foo in my user.dir directory.

FileOutputStream out = new FileOutputStream("foo.txt");     
out.write("foo".getBytes());
out.close();

If I want this file to be created in another directory, that is not the dir that I have ran java command I need to use System.setProperty, in my case I want

System.setProperty("user.dir", System.getProperty("jboss.server.temp.dir"));

But this doesn't work, I think it's because I'm using FileOutputStream instead of File

If I run WildFly through eclipse plugin, I can set a working directory path, this way everything works as expected. See the image

enter image description here

I've tried to put -Duser.dir=%JBOSS_HOME%\standalone\tmp inside standalone.bat script, but doesn't affect at all. The actual working directory is %JBOSS_HOME%\bin, the place where java -jar is called

My question: How can I change the working directory for java running on wildfly and working for FileOutputStream?

Johnny Willer
  • 3,717
  • 3
  • 27
  • 51
  • http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java - in short, you can't do it reliably. I would use a different variable (system or environment) – stdunbar May 11 '16 at 20:57
  • @stdunbar I understand that this has not a trivial solution, and based on the difficult that I'm having to find answers I must be going on the wrong way. How can I make the programmer to not be awere of absolut paths or system variables on FileOutputStream constructor? And why with that option in eclipse plugin works? thank you – Johnny Willer May 11 '16 at 21:04
  • Ultimately the question is where do you want to put the file? Can you safely assume that the O/S user running Wildfly will have permission to do this? I might do a "FileOutputStream out = new FileOutputStream(System.getProperty("java.io.tmpdir" + System.getProperty("file.separator ") + "foo.txt");" to put the file in the **Java** temporary directory, handling Windows vs. Unix. But I have to ask - why does a web server write a file? Shouldn't it stream it back? If it is send by a user, shouldn't it go, for example, in a database? – stdunbar May 11 '16 at 21:14
  • I want to put a file in a temporary place like wildfly /tmp dir. It will be some files for inter-systems integration, one system can read this files for a while. There are many systems on my company using new FileOutputStream("foo.txt"); and I should find a solution that don't aware to paths in the source code. – Johnny Willer May 12 '16 at 11:35

0 Answers0