Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java runtime get result output from prompt problem with a larger output in type

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java runtime get result output from prompt problem with a larger output in type

    I am using java process to start a system command in windows

    Runtime r = Runtime.getRuntime();
    Process pr = r.exec(cmdString);

    I want to get the prompt out put from cmdString = "cmd /c type fileSmallSize"->>>> It is ok the have the content of the file when file is small.
    However, for a large file java process will hang and no Exception occurred, what is the problem?

    The easiest testing you can try on the logging.properties file in java.

    Need help to find out

    public static String executeCmdAndReturnPromptResult(String cmdString)
    throws Exception {
    LOGGER.entering(CLASSNAME,
    "Entering executeCmdAndReturnPromptResult()", cmdString);

    String cmd = cmdString;
    LOGGER.info("The system command: " + cmd);
    Runtime r = Runtime.getRuntime();
    Process pr = r.exec(cmd);
    LOGGER.info("Error: " +getStringFromInputStream(pr.getErrorStream()));
    int exitVal = pr.waitFor();
    // System.out.println("ExitCode cmd return = " + exitVal);
    LOGGER.info("ExitCode cmd return = " + exitVal);

    // String s = "";
    java.io.InputStream input = pr.getInputStream();
    BufferedInputStream buffer = new BufferedInputStream(input);
    BufferedReader commandResult = new BufferedReader(
    new InputStreamReader(buffer));
    StringBuilder sb = new StringBuilder();
    String line = "";
    try {
    while ((line = commandResult.readLine()) != null) {
    sb.append(line+ "\n") ;
    System.out.println("Emcli info: "+ sb.toString());
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    if (commandResult!=null){
    commandResult.close();
    }
    }
    System.out.println("The prompt from the cmd -> " + sb.toString());

    LOGGER.exiting(CLASSNAME, "Exiting executeCmdAndReturnPromptResult()",
    sb.toString());

    return sb.toString().trim();

    }

    It seemed to me that the bufferSize is limited so that I can only have it less than a default one, how to increase it?

    My question now is how to increase the size of buffer in order to read a larger InputStream ?
    BufferedInputStream() default size is
    private static int defaultCharBufferSize = 8192;
    private static int defaultExpectedLineLength = 80;

    How to make it larger and working? I tried to increase the defaultCharBufferSize to 500000000 but it did not help!
    Last edited by kingwang98; August 13th, 2014 at 04:29 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java runtime get result output from prompt problem with a larger output in type

    First, please wrap all code in the code tags to preserve formatting as explained here: Forum Guidelines

    Second, I would recommend reading When Runtime.exec() won't | JavaWorld

    Lastly, it seems your exec command simply reads and prints the contents of a file. Why use Runtime exec rather than reading the file using java IO (which is platform independent)?

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java runtime get result output from prompt problem with a larger output in type

    Quote Originally Posted by copeg View Post
    First, please wrap all code in the code tags to preserve formatting as explained here: Forum Guidelines

    Second, I would recommend reading When Runtime.exec() won't | JavaWorld

    Lastly, it seems your exec command simply reads and prints the contents of a file. Why use Runtime exec rather than reading the file using java IO (which is platform independent)?
    Thank you for your information. The problem solved on :
    Java run windows system command and get result | DaniWeb

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java runtime get result output from prompt problem with a larger output in type

    Suggested reading: http://www.javaprogrammingforums.com...s-posting.html

Similar Threads

  1. Want to print RESULT word before output
    By naveensrikan in forum Other Programming Languages
    Replies: 5
    Last Post: June 1st, 2014, 01:41 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. For each K, output the smallest palindrome larger than K.
    By alekkk in forum Member Introductions
    Replies: 2
    Last Post: July 23rd, 2011, 02:50 PM
  4. The java-output doesn't give the right result?
    By pinotje in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 31st, 2011, 01:00 PM
  5. [SOLVED] Redirect command prompt output to log file.
    By goldest in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 24th, 2010, 05:26 AM