views:

83

answers:

2

I have a java program, and I need to pass to its main method a parameter that has a length of 8k characters. So when I try to execute this program passing that parameter, It just doesn't execute, but no error message is shown. How can I execute correctly that program?

+1  A: 

The best solution is to store this 8k parameter inside a file and pass the file name as a parameter. Then inside your main method you should open this file and read the 8k characters.

Isac
+3  A: 

It is possible that your shell won't allow to exec a program having an argument list above the system limit. Assuming can modify your java program, you should add an option to get the value of the parameter from a file rather than the command-line.

You can also write a wrapper that will invoke your main:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class MyWrapper
{
    public static void main(String[] args) throws Exception
    {
        FileReader reader = new FileReader(args[0]);

        //assuming the data is the first argument
        args[0] = getStringFromReader(reader);

        //invoke real main
        MyClass.main(args);

    }

    public static String getStringFromReader(Reader reader) throws IOException
    {
        final int BUFFER_SIZE = 4096;
        char[] buffer = new char[BUFFER_SIZE];
        Reader bufferedReader = new BufferedReader(reader, BUFFER_SIZE);
        StringBuffer stringBuffer = new StringBuffer();
        int length = 0;
        while ((length = bufferedReader.read(buffer, 0, BUFFER_SIZE)) != -1)
        {
            stringBuffer.append(buffer, 0, length);
        }
        reader.close();

        return stringBuffer.toString();
    }
}

Then, you only need to call java like this:

java MyWrapper my-file-containing-8k-data [other-args...]

gawi
I can't modify the program in that way, I should get the value from command line.
Dorr
The propose program is not a modification, it's an addition. This is a trick not to have to modify your existing program. You only need to compile it and to add it to your classpath along with your application jar. The basic idea is "if you can call it from the command-line, you can call it from java code".
gawi
Under UNIX, you can also the following trick `java MyClass `cat my-file-containing-8k-data``
gawi
Is it not possible from command line? I can't read any file. It must come from command line.
Dorr
It depends on your system. What shell are you using? I'm using TCSH on Linux and it let me type 8192 characters, no more. The TCSH doc says that "the system limits argument lists to ARG_MAX characters", which is 131072 on my system (as defined in /usr/include/linux/limits.h).
gawi
It is just DOS.
Dorr
The CMD.EXE limit is 8191 characters. See http://support.microsoft.com/kb/830473/en-us
gawi