Search code examples
javanetbeanswrapperexecutablejsmooth

String search breaking when wrapping .jar with JSmooth


I've got an oddball problem here. I've got a little java program that filters Minecraft log files to make them easier to read. On each line of these logs, there are usually multiple instances of the character "§", which returns a hex value of FFFD.

I am filtering out this character (as well as the character following it) using:

currentLine = currentLine.replaceAll("\uFFFD.", "");

Now, when I run the program through NetBeans, it works swell. My lines get outputted looking like this:

CxndyAnnie: Mhm
CxndyAnnie: Sorry

But when I build the .jar file and wrap it into a .exe file using JSmooth, that character no longer gets filtered out when I run the .exe, and my lines come out looking like this:

§e§7[§f$65§7] §1§nCxndyAnnie§e: Mhm
§e§7[§f$65§7] §1§nCxndyAnnie§e: Sorry

(note: the additional square brackets and $65 show up because their filtering is dependent on the special character and it's following character being removed first)

Any ideas why this would no longer work after putting it through JSmooth? Is there a different way to do the text replace that would preserve its function?

By the way, I also attempted to remove this character using

currentLine = currentLine.replaceAll("§.", "");

but that didn't work in Netbeans nor as a .exe.

I'll go ahead and past the full method below:

 public static String[] filterLines(String[] allLines, String filterType, Boolean timeStamps) throws IOException {
    String currentLine = null;
    FileWriter saveFile = new FileWriter("readable.txt");
    String heading;
    String string1 = "[L]";
    String string2 = "[A]";
    String string3 = "[G]";
    if (filterType.equals(string1)) {
        heading = "LOCAL CHAT LOGS ONLY \r\n\r\n";
    }
    else if (filterType.equals(string2)) {
        heading = "ADVERTISING CHAT LOGS ONLY \r\n\r\n";
    }
    else if (filterType.equals(string3)) {
        heading = "GLOBAL CHAT LOGS ONLY \r\n\r\n";
    }
    else {
        heading = "CHAT LINES CONTAINING \"" + filterType + "\" \r\n\r\n";    
    }
    saveFile.write(heading);

    for (int i = 0; i < allLines.length; i++) {
        if ((allLines[i] != null ) && (allLines[i].contains(filterType))) {
            currentLine = allLines[i];
            if (!timeStamps) {
                currentLine = currentLine.replaceAll("\\[..:..:..\\].", "");
            }
            currentLine = currentLine.replaceAll("\\[Client thread/INFO\\]:.", "");
            currentLine = currentLine.replaceAll("\\[CHAT\\].", "");
            currentLine = currentLine.replaceAll("\uFFFD.", "");
            currentLine = currentLine.replaceAll("\\[A\\].", "");
            currentLine = currentLine.replaceAll("\\[L\\].", "");
            currentLine = currentLine.replaceAll("\\[G\\].", "");
            currentLine = currentLine.replaceAll("\\[\\$..\\].", "");
            currentLine = currentLine.replaceAll(".>", ":");
            currentLine = currentLine.replaceAll("\\[\\$100\\].", "");
            saveFile.write(currentLine + "\r\n");
            //System.out.println(currentLine);
        }
    }
    saveFile.close();
    ProcessBuilder openFile = new ProcessBuilder("Notepad.exe", "readable.txt");
    openFile.start();
    return allLines;
}

FINAL EDIT

Just in case anyone stumbles across this and needs to know what finally worked, here's the snippet of code where I pull the lines from the file and re-encode it to work:

    BufferedReader fileLines;
    fileLines = new BufferedReader(new FileReader(file));
    String[] allLines = new String[numLines];
    int i=0;
    while ((line = fileLines.readLine()) != null) {
        byte[] bLine = line.getBytes();
        String convLine = new String(bLine, Charset.forName("UTF-8"));
        allLines[i] = convLine;
        i++;
    }

Solution

  • I also had a problem like this in the past with minecroft logs, I don’t remember the exact details, but the issue came down to a file format problem, where UTF8 encoding worked correctly but some other text encoding including the system default did not work correctly.

    First:

    Make sure that you specify UTF8 encoding when reading the byteArray from file so that allLines contains the correct info like so:

    Path fileLocation = Paths.get("C:/myFileLocation/logs.txt");
    byte[] data = Files.readAllBytes(fileLocation);
    String allLines = new String(data , Charset.forName("UTF-8"));
    

    Second:

    Using \uFFFD is not going to work, because \uFFFD is only used to replace an incoming character whose value is unknown or unrepresentable in Unicode.

    However if you used the correct encoding (shown in my first point) then \uFFFD is not necessary because the value § is known in unicode so you can simply use

    currentLine.replaceAll("§", "");

    or specifically use the actual unicode string for that character U+00A7 like so

    currentLine.replaceAll("\u00A7", "");

    or just use both those lines in your code.