jeudi 27 avril 2017

Changing CWD for Method Invoked via Java.Lang.Reflect

I have a Java servlet that uses Reflection to invoke other Java methods. Some Java methods that I invoke generate files, and I want to be able to move those files to another location.

Currently, if an absolute path isn't supplied in the method (e.g. PrintWriter outFile = new PrintWriter("prog.out")), the output file gets thrown into the bin directory of my Tomcat installation.

I've tried setting the CWD with the following code, where myDataDir is the directory I want the output files to go into:

File directory;
directory = new File(myDataDir.getPath()).getAbsoluteFile();
if (directory.exists() || directory.mkdirs()) {
    System.setProperty("user.dir", directory.getAbsolutePath());
}

I've even gone so far as to go into the code for the Java method I've written and print out what "user.dir" is from there. Even there it tells me that everything is set up properly (e.g. "user.dir" == /path/that/i/want). Nevertheless, when I invoke the method with reflection, the output file gets put in the bin directory.

Has anyone experienced anything like this before? Any pointers or guidance would be greatly appreciated.

Some details:

  • Running on Redhat 7.3
  • Using Tomcat version 8.0.36
  • Building with Maven 3.3.9
  • Java 1.7.0

Thanks!





Aucun commentaire:

Enregistrer un commentaire