Skip to content

Commit b3b8a44

Browse files
committed
File with error messages must be found under various circumstances
1 parent 2c0b9da commit b3b8a44

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/org/deepjava/host/ErrorReporter.java

+31-20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.deepjava.host;
2020
/**
2121
changes:
22+
<br>2022-03-25, OST/GRAU error file must be found in all use cases
2223
<br>11-09-12, NTB/MILR error(int errNr, String additionalInfo): printing errNr, errMsg and additional informations, handling if class is into a jar
2324
<br>09-04-22, NTB/ED error(int errNr, String errMsg): printing errNr, errMsg
2425
<br>09-03-23, NTB/ED extension of error(int errNr, String errMsg)
@@ -31,6 +32,8 @@
3132
import java.io.IOException;
3233
import java.io.InputStreamReader;
3334
import java.io.PrintStream;
35+
import java.io.UnsupportedEncodingException;
36+
import java.net.URLDecoder;
3437
import java.util.jar.JarFile;
3538
import java.util.zip.ZipEntry;
3639

@@ -40,6 +43,7 @@
4043
*/
4144
public class ErrorReporter {
4245
public static final ErrorReporter reporter;
46+
private static PrintStream vrb = StdStreams.vrb;
4347
private PrintStream errPrStream;
4448
private String errorMsgFilePath = "rsc/ErrorMsg.txt";
4549
private JarFile jar;
@@ -57,29 +61,36 @@ public class ErrorReporter {
5761
private ErrorReporter() {
5862
clear();
5963
errPrStream = StdStreams.err;
60-
String home = "";
61-
62-
if(System.getProperty("os.name").contains("Windows")){ // Running on Microsoft Windows
63-
home = getClass().getProtectionDomain().getCodeSource().getLocation().toString().substring(6); // get jar name
64-
// we have to remove the first 6 characters of the returned _absolute_ path to the JAR file,
65-
// because the string starts with "file:/" which is not a valid file name!
66-
// Example: file:/I:\eclipse\..
67-
} else { // Running on Linux, Mac OS X or another UNIX system
68-
home = getClass().getProtectionDomain().getCodeSource().getLocation().toString().substring(5); // get jar name
69-
// we have to remove the first 5 characters of the returned _absolute_ path to the JAR file,
70-
// because the string starts with "file:" which is not a valid file name!
71-
// Example: file:/opt/eclipse/..
64+
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
65+
String url = null;
66+
try {
67+
url = URLDecoder.decode(path, "UTF-8");
68+
} catch (UnsupportedEncodingException e) {
69+
vrb.println("Error message file cannot be loaded, URL syntax error ");
7270
}
73-
74-
if(home.endsWith("jar")) { // used when running as an eclipse plugin
75-
try {
76-
jar = new JarFile(home);
77-
} catch (IOException e) {
78-
e.printStackTrace(errPrStream);
79-
}
80-
71+
// errPrStream.println(url);
72+
while (url.contains("..")) { // used when eclipse launched from within eclipse workspace
73+
int index = url.indexOf("..");
74+
char ch = url.charAt(index - 1);
75+
int index1 = url.lastIndexOf(ch, index - 2);
76+
String p1 = url.substring(0, index1);
77+
String p2 = url.substring(index + 2);
78+
url = p1 + p2;
79+
}
80+
if (url.endsWith("jar")) { // used when running as an eclipse plugin
81+
try {
82+
jar = new JarFile(url);
83+
} catch (IOException e) {
84+
vrb.println("Error message file cannot be loaded");
85+
}
86+
} else {
87+
if (url.endsWith("bin/")) { // used when started directly in eclipse (e.g. with the Testlauncher)
88+
url = url.substring(0, url.length() - 4);
89+
}
90+
errorMsgFilePath = url + errorMsgFilePath;
8191
}
8292
this.maxNofErrors = Integer.MAX_VALUE;
93+
// errPrStream.println(errorMsgFilePath);
8394
}
8495

8596
public void setMaxNrOfErrors(int maxNofErrors) {

0 commit comments

Comments
 (0)