Read Resource file "RDMFieldDictionary" have some problem

image

In this method

QSConsumer.java

public void init()
{
String fieldDictionaryFilename = "../etc/RDM/RDMFieldDictionary";
String enumDictionaryFilename = "../etc/RDM/enumtype.def";
try
{
GenericOMMParser.initializeDictionary(fieldDictionaryFilename, enumDictionaryFilename);
}
catch (DictionaryException ex)
{
System.out.println("ERROR: Unable to initialize dictionaries");
System.out.println(ex.getMessage());
if(ex.getCause() != null)
System.err.println(": " + ex.getCause().getMessage());
cleanup();
return;
}

}

STEP1:

In this source code ,it wants to read RDMFieldDictionary ,

String fieldDictionaryFilename = "../etc/RDM/RDMFieldDictionary";

and i change the code to get the file's path like this

String fieldDictionaryFilename = Thread.currentThread().getContextClassLoader().getResource("rfa/RDMFieldDictionary").getFile();

and i got the result like this

/D:/workspace/xuntou-base-market/xuntou-base-market-facade/target/classes/rfa/RDMFieldDictionary

STEP2:

i start the project user Intellij (a IDE like Eclipse) in my own computer, it runs very well

public class RfaSubscribeThread implements Runnable{

public void run() {

// Create a demo instance
QSConsumer demo = new QSConsumer();

// Startup and initialization
demo.init();
//...
}
}

STEP3:

But when the project has become a Jar File,and start up on the Linux Mechine ,it give me some wrong info like this

ERROR: Unable to initialize dictionaries
ERROR: Check if files file:/usr/local/xuntou/base-market/xuntou-base-market-facade/lib/xuntou-base-market-facade-1.1.9-SNAPSHOT.jar!/rfa/RDMFieldDictionary and file:/usr/local/xuntou/base-market/xuntou-base-market-facade/lib/xuntou-base-market-facade-1.1.9-SNAPSHOT.jar!/rfa/enumtype.def exist and are readable.
: Dictionary file file:/usr/local/xuntou/base-market/xuntou-base-market-facade/lib/xuntou-base-market-facade-1.1.9-SNAPSHOT.jar!/rfa/RDMFieldDictionary does not exist.
RWF: 14.1
RFA: Version = 8.1.0.E2.all, Date = Wed May 02 18:30:50 CST 2018, Jar Path = /usr/local/xuntou/base-market/xuntou-base-market-facade/lib/rfa-1.0.0.jar, Jar Size = 1619345


class com.xuntou.platform.market.rfa.quickstart.QuickStartConsumer.QSConsumer exiting

STEP4:

I seach the internet ,fount in the jar file , use "new File("FilePath")" to get file is not right

i want to change ,but the source code

GenericOMMParser.initializeDictionary(fieldDictionaryFilename, enumDictionaryFilename);

only use "new File('FilePath')" to read the resource file

So, what can i do to avoid the problem.

TIPS: I can't change the source code, it's provided by your company

image

expect your help, thank you very much!

Best Answer

Answers

  • Hello @peng.xiaoliang

    I can replicate the issue with the given code. When the application keeps Dictionary files with resources folder and build jar file, the following code cannot access Dictionary files inside jar file:

    image

    fieldDictionaryFilename = Thread.currentThread().getContextClassLoader().getResource("etc/RDMFieldDictionary").getFile();
    enumDictionaryFilename = Thread.currentThread().getContextClassLoader().getResource("etc/enumtype.def").getFile();

    Then I got the following error message when I try to run the jar file.

    ERROR: Unable to initialize dictionaries
    ERROR: Check if files file:/D:/Project/SRs/AHS_27324/Code/RFA_code/out/artifacts
    /RFA_OMM_AHS_27324_jar/RFA_OMM_AHS_27324.jar!/etc/RDMFieldDictionary and file:/D
    :/Project/SRs/AHS_27324/Code/RFA_code/out/artifacts/RFA_OMM_AHS_27324_jar/RFA_OM
    M_AHS_27324.jar!/etc/enumtype.def exist and are readable.
    : Dictionary file file:/D:/Project/SRs/AHS_27324/Code/RFA_code/out/artifacts/RFA
    _OMM_AHS_27324_jar/RFA_OMM_AHS_27324.jar!/etc/RDMFieldDictionary does not exist.

    I will check this behavior in detail and let you know updates.

  • it confused me. I still don't know how to solve this problem. The jar file and source code is provided, the only way to read the dictionary file is use the source code which is provided.

  • Publish the project to the internet must park the source code to JAR file, but in the jar file ,it can't read the dictionary file . The helper website i can't open it .because of the GFW

  • Hello @peng.xiaoliang

    The dictionary files are not designed to be inside the application Jar file because they are designed to be easy updated by just replacing the files. The application should place dictionary files outside the Jar file and read them from that location.