简体   繁体   中英

My JAR doesnt work as when launched by eclipse

My program uses large text files (45MB) to operate and runs fine in eclipse. After being exported into a jar, it does not work properly when accessing the large source files. There are no problems with small files (300kB)

Eclipse: freezes for 10 seconds and then is ready with the file loaded.

Jar: Freezes for 5 seconds and nothing happens.

What can I change about the JAR to function properly?

EDIT:

public String [] RetreiveTokens(){
    String path = Main_Win.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    String decodedPath = null;
    try {
        decodedPath = URLDecoder.decode(path, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    String FileName=decodedPath+"languages/D3.txt";//LangPath+SourceFileLists.get(i);

    String content = null;
    boolean DictCC = false;
    int ContentsAddress=0;
    try {

        BufferedReader fin;

        String lineFromFile="";
        try {
            fin = new BufferedReader(new FileReader(FileName));
            //System.out.println("cc");
            lineFromFile = fin.readLine();
            lineFromFile=lineFromFile.substring(3);
            if(lineFromFile.substring(0,1).equals("#")){
                DictCC=true;
                for(ContentsAddress=0; fin.readLine().length()>0; ContentsAddress++);
                ContentsAddress+=2;
                //System.out.println(ContentsAddress);
            }
            fin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //System.out.println(lineFromFile);
        //if(!DictCC){
        content = new Scanner ( new File (FileName), "UTF-8").useDelimiter("\\Z").next();
        //}
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String[] tokens=null;
    //String[][] Words_1=new String[2][32];;

    if(!DictCC){
        content=content.substring(1);
        content=content.replace("\r\n\r\n", "\r\n");
        tokens = content.split("\r\n");
    }
    else {
        content = content.split("\r\n")[ContentsAddress];
        tokens = content.split("    ");
    }

    for (int t=0; t<tokens.length; t++){
        if(tokens[t].contains("\n")){
            int index = nthOccurrence(tokens[t], '\n', 0);
            tokens[t]=tokens[t].substring(index);
        }
        if(tokens[t].contains("[")){
            tokens[t]=tokens[t].replace(" [", "[");
            tokens[t]=tokens[t].replace("] ", "]");
            tokens[t]=tokens[t].replaceAll("\\[.*\\]", "");
        }
        if(tokens[t].contains("<")&&tokens[t].contains(">")){
            tokens[t]=tokens[t].replace(" <", "<");
            tokens[t]=tokens[t].replace("> ", ">");
            tokens[t]=tokens[t].replaceAll("\\<.*\\>", "");
        }
        tokens[t]=tokens[t].replaceAll("[?¿!¡,.;/'{}]", "");
        tokens[t]=tokens[t].replace("\n", "").replace("\r", "");

    }
    return tokens;
}

I think that your problem is shortage of heap memory. You are running the applicaiton by double clicking of its jar, so javaw that does not have STDOUT and STDERR is used.

Open command prompt and run your application by typing java -jar yourjar.jar where yourjar.jar is actual path to your jar.

You will see OutOfMemoryError . Now you should increase the memory. Add parameter -Xmx2048M to your execution path, ie:

java -Xmx2048M -jar yourjar.jar

I hope this will help. If not find better number. Obviously this line means that your application will be able to use up to 2G of heap.

Now the question is "why do you need so much memory?" Try to review your code and see what's happens. Probably you read whole file into the memory? Do you need this? Etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM