I have two text files: PatientForms.txt and MasterCodeList.txt.
Each file holds a JSON string on each line. I use the following method to read in the contents of the file and store it in a SortedMap for example.
Here is my ServiceCode.java code:
public class ServiceCode {
//class variables
@SerializedName("code")
private String code;
@SerializedName("description")
private String description;
@SerializedName("cost")
private double fullRetailPrice;
//constructor: default - creates an empty code that can be modified by user
public ServiceCode() {
setCode("Empty");
setDescription("Empty");
setFullRetailPrice(0.0);
}
//constructor: non-default - creates a code with values passed to it for code, description, and fullRetailPrice
public ServiceCode(String code, String description, double fullRetailPrice) {
setCode(code);
setDescription(description);
setFullRetailPrice(fullRetailPrice);
}
//setters
public void setCode(String code) { this.code = code; }
public void setDescription(String description) { this.description = description; }
public void setFullRetailPrice(double fullRetailPrice) { this.fullRetailPrice = fullRetailPrice; }
//getters
public String getCode() { return code; }
public String getDescription() { return description; }
public double getFullRetailPrice() { return fullRetailPrice; }
Here is my method that reads in the file and places it in a map:
public class CodeFileHandler {
//static final File dir = new File(".");
static final Gson gson = new Gson();
public SortedMap<String, ServiceCode> loadCodeList() {
SortedMap<String, ServiceCode> map = new TreeMap<>();
List<String> codeStrings = new ArrayList<>();
try {
String loc = "MasterCodeList.txt";
BufferedReader br = new BufferedReader(new FileReader(loc));
for(String line; (line = br.readLine()) != null; ) {
codeStrings.add(line);
System.out.println(line);
}
// line is not visible here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (String s : codeStrings) {
//used to show that string is a valid JSON format
System.out.println(s);
ServiceCode c = gson.fromJson(s, ServiceCode.class);
map.put(c.getCode(), c);
}
return map;
}
So, the problem is that this works without error in IntelliJ, but when I run it from my jar file I get an error. The string as output by the IDE when I run the program shows a valid format:
{"code":"D0120","description":"Periodic oral evaluation-established pt","cost":39.0}
When I run it from the jar file I get this:
F:\BLCHC_MCv1\out\artifacts\BLCHC_MCv1_jar>java -jar BLCHC_MCv1.jar {"code":"D0120","description":"Periodic oral evaluation-established pt","cost":39.0} Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224) at com.google.gson.Gson.fromJson(Gson.java:887) at com.google.gson.Gson.fromJson(Gson.java:852) at com.google.gson.Gson.fromJson(Gson.java:801) at com.google.gson.Gson.fromJson(Gson.java:773) at com.olsen.blchc.CodeFileHandler.loadCodeList(CodeFileHandler.java:39)
All sources I have looked at for the last couple of days point to the string being read in having a " in place of a { at the beginning. This is not the case.
Does anybody know why this would happen? It has no problem loading from a different file when I access PatientForms.txt, even when it is run from the jar.
UPDATE!
I changed my loadCodeList method to this:
public void loadCodeList() {
try {
String loc = "MasterCodeList.txt";
BufferedReader br = new BufferedReader(new FileReader(loc));
for(String line; (line = br.readLine()) != null; ) {
ServiceCode s = gson.fromJson(line, ServiceCode.class);
codeMap.put(s.getCode(), s);
}
// line is not visible here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
My code reads in each line and converts it to a ServiceCode object from the string in JSON format, using gson.fromJson(String, ServiceCode);
where String is the JSON string read in from the file.
OUTPUT FROM IntelliJ IDE
In the IDE I can run everything without fail and access the ServiceCode objects in my SortedMap.
JAR FILE OUTPUT JAR FILE CONSOLE OUTPUT
Am I not packaging my jar file correctly? I included the Gson Jar inside of my Jar directory.