简体   繁体   中英

Jar file not accepting huge string in java

What is the best way of passing a string (arg1 in case of code below) with about 800K characters (yes, that's a huge number) to a java jar. Following is the code I am using to invoke the jar file:

p = Runtime.getRuntime().exec(jrePath+"/bin/java -jar C:/folder/myjar.jar methodName" + arg1);

ALternately, how can I create a jar file to accept one String input and one byte[] input in main{ in void main(String args[]) }

Or any other ideas? The requirement is to somehow pass the huge String / byte[] of String to a java jar file that I am creating

As mentioned in this question , there is a maximum argument length set by the operating system. Seeing as this argument is 800K characters, its fairly safe to say that you have exceeded this max value on most computers. To get around this, you can write arg1 to a temp file using the built in API:

final File temp;
try {
    temp = File.createTempFile("temp-string", ".tmp");
} catch (final IOException e){
    //TODO handle error
    return;
}
try (final BufferedWriter writer = new BufferedWriter(new FileWriter(temp))) {
    writer.write(arg1);
} catch (final IOException e){
    //TODO handle error
    return;
}
try {
    // run process and wait for completion
    final Process process = Runtime.getRuntime().exec(
        jrePath + "/bin/java -jar C:/folder/myjar.jar methodName " +
        temp.getAbsolutePath());
    final int exitCode = process.waitFor();
    if (exitCode != 0) {
        //TODO handle error
    }
} catch (final IOException | InterruptedException e){
    //TODO handle error
    return;
}
if (!file.delete()) {
    //TODO handle error
}

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