Parameter length too long
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?开发者_运维百科
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...]
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.
精彩评论