D
D
Daniil Demidko2016-04-06 10:04:31
Java
Daniil Demidko, 2016-04-06 10:04:31

Console in java?

A small question about JRE and standard output to the console.
For example, I compile and package this simple and completely pointless code into a runnable jar:

package Experimental;

import java.io.*;

public class Main {

  public static void main(String[] args) {
  
    for(char i=0; i<512; ++i) {
            System.out.write(i);
      System.out.write('\n');
    }
    
  }
  
}

And I run this .jar file with a mouse click from under windows - the result is zero.
I try to run from under cmd:
java -jar MyJar.jar - everything works fine.
Now I'm rewriting this code, but redirecting the output to a file:
package Experimental;

import java.io.*;

public class Main {

  public static void main(String[] args) {
  
    try (FileOutputStream out=new FileOutputStream("out.txt");) {
    
      for(char i=0; i<512; ++i) {
        out.write(i);
        out.write('\n');
      }
      
    }
    
    catch(IOException e) {
      System.out.print("Error!");
    }
  }
  
}

And again I launch it by clicking on .jar - this time everything works - out.txt file appears
Now the question is - why doesn't the console start on mouse click?
JRE not friendly with windows?
But from under cmd after all the conclusion works?
Why is that?
Everything, the issue is resolved, I found a solution on the official website. Link: https://www.java.com/en/download/help/javaconsole.xml

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2016-04-06
@Daniro_San

I will assume that Java does not know how to launch the cmd shell for sure. Write a batch file with the content "java -jar MyJar.jar" and use it to run your program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question