Java: System.out.println () should be written to the console, but this is not

I am trying to write a Java console application, the code is pretty simple:

public class ConsoleTest { public static void main(String[] args) { System.out.println("test"); } } 

If I run this application from Eclipse, I see " test " in the "Eclipse Console", but if I export my application as a "Runnable JAR file" and run it from Windows XP cmd.exe , then nothing is displayed on the console.

In a safe place, I tried checking System.console() , it returns null .

What could be wrong?

+8
java console
source share
6 answers

How do you run your program beyond eclipse?

You should use a command line like

java -jar yourjar.jar

or

java -cp yourjar.jar ConsoleTest

If you sometimes use javaw , then console output will not be created. STDOUT javaw is null . This is probably what happens when you click on your jar file.

+15
source share

You need to run the jar file from the command line.

If you double-click on it, you will not be able to see how command line operations are performed in the background. Jar files are usually double-clicked only when they include the GUI.

To run the jar file from the command line, simply do the following:

 java -jar ConsoleTest.jar 

Assuming you set environment variables for java.exe and there is a jar file in the current directory.

If this does not work, it may not be your code error. There is also the possibility that the manifest file pointing to the Main class was not configured correctly.

+4
source share

Try compiling it into a class from the command line and then running it. It works?

 javac ConsoleTest.java java ConsoleTest 
+2
source share

Most likely, your jar executable does not work as you expect, and your ConsoleTest class does not actually work.

+1
source share

Like what Hovercraft Full Of Eels wrote, compile the .java file using cmd.exe using the following command:

 javac ConsoleTest.java 

This will then create a .class file, and we want to compile it with this command:

 java ConsoleTest 

Then it should display the β€œtest” output.

0
source share

you need to install java jdk and add the root of the directory containing the java.exe file to your system directories, you will find additional information in the jdk installation guide.

and then run the file in the console using the command

 > java file.java 
-one
source share

All Articles