0

I have jar/zip file contains properties file called accord.properties under classes folder.

Zip/Jar file:
  +classes
      +accord.properties

I am reading file as:

from java.util import Properties
from java.io import File, FileInputStream
def loadPropsFil(propsFil):
    print(propsFil)    
    inStream = FileInputStream(propsFil)
    propFil = Properties()
    propFil.load(inStream) 
    return propFil
pFile = loadPropsFil("/accord.properties")
print(pFile)

while running in Tomcat server, I am getting error as

Exception stack is: 1. accord.properties (No such file or directory) (java.io.FileNotFoundException) java.io.FileInputStream:-2 (null) 
2. null(org.python.core.PyException) org.python.core.Py:512 (null) 
3. java.io.FileNotFoundException: java.io.FileNotFoundException: accord.properties (No such file or directory) in <script> at line number 34 (javax.script.ScriptException)

Tried with

pFile = loadPropsFil("accord.properties")

and

pFile = loadPropsFil("classpath:accord.properties")

same error.

EDIT

inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("accord.properties")
strProp = Properties().load(inStream) # line 38
options.outputfile=strProp.getProperty("OUTPUT_DIR")

Here inStream gives null and causes NullPointer Exception.

Error:

 java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 38 (javax.script.ScriptException)

1 Answer 1

1

You can't access files in a JAR like a normal file using FileInputStream. Instead, you need to access them as resources using Class.getResourceAsStream(). Try something like this:

inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("accord.properties")

I'm glad you were able to figure out how to call getResourceStream. I'm not sure what the "Mark invalid" error means. This works fine for me:

    $ CLASSPATH=hello.jar:$CLASSPATH jython
    Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
    [Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from java.lang import ClassLoader
    >>> from java.io import InputStreamReader, BufferedReader
    >>> inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("hello.txt")
    >>> reader = BufferedReader(InputStreamReader(inStream))
    >>> reader.readLine()
    u'Hello!'

Since hello.jar contains the file hello.txt which has the single line Hello!, the above is my expected output.

Sign up to request clarification or add additional context in comments.

3 Comments

@akb - I don't get an error running your code. pFile.getProperty("OUTPUT_DIR") returns u'/home/mulesoft/report/' on my machine if I use the properties file you posted. You don't still have a / before accord.properties do you?
@akb - Try removing one line at a time to figure out which is causing the problem. You might try switching to an XML properties file instead and see if that fixes the problem. Also, you could try generating the properties file using the store or storeToXML methods on the Properties class.
Now I am getting inStream is null, but my property file is exists under the jar classes folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.