0

I am trying to package a JAR where some classes may need to read a txt file. I include the txt file in the root directory of the JAR package.

This JAR package gets loaded on Pl/Java.

However, when trying to execute in PLjava, it returns the following error:

ERROR:  java.lang.SecurityException: read on datafile.txt
Larry
  • 11,439
  • 15
  • 61
  • 84
  • 1
    How are you trying to read the file? Can you post the code? – Simone Gianni Sep 06 '11 at 13:23
  • 1
    How is the file read? Show us the code. – JB Nizet Sep 06 '11 at 13:24
  • I can’t, I am using another package that asks me for the path to the file, and then it reads it, etc. I usually just put the file in the root directory of the application and it works fine.. – Larry Sep 06 '11 at 13:26
  • What would seem to be likely causes or possible solutions to this? – Larry Sep 06 '11 at 13:29
  • *"I am using another package that asks me for the path to the file"* What package? Where are the JavaDocs (what URL) for the method being used to load the image? – Andrew Thompson Sep 06 '11 at 13:35
  • @Larry: There are no files in a jar. There are resources, that must be loaded using ClassLoader.getResourceAsStream. If the library tries to open them using a File, it won't work, whether in PL/Java or not. – JB Nizet Sep 06 '11 at 13:38
  • @Andrew its not a release package, it was written by my company, and I don’t seem to have the source code at the moment. – Larry Sep 06 '11 at 13:42
  • I’ve got something like in the JavaDoc: `static void init(java.lang.String pathToFile) Read database connection configuration details from local file` – Larry Sep 06 '11 at 13:47
  • @Larry. As I said, it seems that the package tries to read from a file on the file system. A resource in a jar is not the same thing, and this package won't be able to access your datafile.txt in the jar. – JB Nizet Sep 06 '11 at 14:01
  • @JB That’s correct. What I tried now was to create new project and import my package. When I put the datafile.txt in the new project root it works, but when it is not there (even though it may be packaged in the JAR) it doesn’t work. So the question is: in PLJava, I need to know where the root directory is where it is executing the JARs and perhaps I could place the txt file there? How could I find out this directory? Or what really is then the solution to this? – Larry Sep 06 '11 at 14:25
  • Are you sure there is not also a `static void init(java.net.URL url)` or `static void init(java.io.InputStream inputStream)` method? Accepting a `String` that represents a `File` is bad practice (IMO), but it would be a fatally flawed API if the bytes are only for read and the two forms I suggested are not available. – Andrew Thompson Sep 06 '11 at 14:32
  • PL/Java blocks filesystem access. The external package you're using can't be used in PL/Java due to security restrictions PL/Java imposes. You have to embed the file in the jar and use ClassLoader.getResourceAsStream to load it. – JB Nizet Sep 06 '11 at 14:33
  • It certainly does not have what Andrew suggested (just String pathToFile) - I agree bad practice. So what are my options to work around this? – Larry Sep 06 '11 at 14:37

1 Answers1

0

I found this page in the google cache (cached pl/java page) which indicates that PL/Java blocks all filesystem access by default as part of its design.

mcfinnigan
  • 11,442
  • 35
  • 28
  • It seems extraordinary that they would extend the security to resources within Jars that are (supposedly) on the run-time class-path of the application. If that were the case, it could not load `.class` files from those same Jars. – Andrew Thompson Sep 06 '11 at 13:34