You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by A Z <po...@live.com.au> on 2018/09/19 05:34:13 UTC

Netbeans 9 Console Capturing Problem.

//I am encountering my Netbeans 9 console capture problem via the following:

package Project;

import java.util.LinkedList;
import static java.lang.System.out;

public class Main
{
 public static void main(String ... args)
 {
  out.println();

  out.println("Primary Main program has begun.");

  out.println();

  //Designed to intentionally fill up memory.
  LinkedList<Object> list = new LinkedList<Object>();

  while(true)
  {
  list.add(new Object());
  }
 }

}//End of Class

package Project;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import static java.lang.System.out;

public class Launcher
{
   public static void main(String ... args)
   {
     try
     {
     //This is not a blocking command.
     ProcessBuilder builder = new ProcessBuilder("java", "-Xmx8192m", "-cp", "MemoryLauncher.jar", "Project.Main");

     builder.inheritIO();

     Process process = builder.start();

     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

     String line;

     out.println();

      do
     {
     line = reader.readLine();

      if(line instanceof String)
       {
        out.println(line);
       }

     }
     while(line instanceof String);

     reader.close();

     }
     catch (Exception exception)
     {
     exception.printStackTrace();
     }
     out.println("Launcher program has finished.");

     out.println();

   }  //End of Main.
} //End of Class.

Re: Netbeans 9 Console Capturing Problem.

Posted by Geertjan Wielenga <ge...@googlemail.com.INVALID>.
Please don't send code around that is "designed to intentionally fill up
memory".

To respond to your problem, a first question to ask is whether
"MemoryLauncher.jar" exists anywhere? I doubt it.

You'll need to build the project (right-click the project and choose
Build), which means that that JAR (assuming the name of the project is
called "MemoryLauncher") will be created and found in the "dist" folder.
Check to make sure its there (in the Files window).

You'll then need to refer to it in your ProcessBuilder code
as "dist/MemoryLauncher.jar".

In the "manifest.mf", you'll need to register your main class:

Main-Class: Project.Main

Also, use System.out.println instead of out.println.

So, after changing the above in your code, rebuild, check you have the JAR
in your "dist" folder, and then run the Launcher.

I now see:

run:

Launcher program has finished.

Primary Main program has begun.
BUILD SUCCESSFUL (total time: 0 seconds)

The code:

package Project;

public class Main {

    public static void main(String... args) {
        System.out.println("Primary Main program has begun.");
    }

}

package Project;

import java.io.InputStreamReader;
import java.io.BufferedReader;

public class Launcher {

    public static void main(String... args) {
        try {
            ProcessBuilder builder = new ProcessBuilder(
                    "java", "-Xmx8192m", "-cp", "dist/MemoryLauncher.jar",
"Project.Main");
            builder.inheritIO();
            Process process = builder.start();
            BufferedReader reader = new BufferedReader(new
InputStreamReader(process.getInputStream()));
            String line;
            System.out.println();
            do {
                line = reader.readLine();
                if (line instanceof String) {
                    System.out.println(line);
                }

            } while (line instanceof String);
            reader.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println("Launcher program has finished.");
        System.out.println();

    }
}

Thanks,

Gj





On Wed, Sep 19, 2018 at 7:34 AM, A Z <po...@live.com.au> wrote:

> //I am encountering my Netbeans 9 console capture problem via the
> following:
>
> package Project;
>
> import java.util.LinkedList;
> import static java.lang.System.out;
>
> public class Main
> {
>  public static void main(String ... args)
>  {
>   out.println();
>
>   out.println("Primary Main program has begun.");
>
>   out.println();
>
>   //Designed to intentionally fill up memory.
>   LinkedList<Object> list = new LinkedList<Object>();
>
>   while(true)
>   {
>   list.add(new Object());
>   }
>  }
>
> }//End of Class
>
> package Project;
>
> import java.io.InputStreamReader;
> import java.io.BufferedReader;
> import static java.lang.System.out;
>
> public class Launcher
> {
>    public static void main(String ... args)
>    {
>      try
>      {
>      //This is not a blocking command.
>      ProcessBuilder builder = new ProcessBuilder("java", "-Xmx8192m",
> "-cp", "MemoryLauncher.jar", "Project.Main");
>
>      builder.inheritIO();
>
>      Process process = builder.start();
>
>      BufferedReader reader = new BufferedReader(new
> InputStreamReader(process.getInputStream()));
>
>      String line;
>
>      out.println();
>
>       do
>      {
>      line = reader.readLine();
>
>       if(line instanceof String)
>        {
>         out.println(line);
>        }
>
>      }
>      while(line instanceof String);
>
>      reader.close();
>
>      }
>      catch (Exception exception)
>      {
>      exception.printStackTrace();
>      }
>      out.println("Launcher program has finished.");
>
>      out.println();
>
>    }  //End of Main.
> } //End of Class.
>