Suppose I've created an executable jar from a code where I have used
System.out.println()
When we run the executable jar, there is no console. So, what happens to this line? How does java handle this situation?
EDIT 01:
NOTE: The situation is when I don't use a console to run the jar nor associate any console with it anyhow.
EDIT 02: Making things clearer:
I know that nothing will be printed anywhere as there is no console..! I want to know how java handle this line in this case? Is this line omitted when generating the bytecode for a executable jar? Or is this line just overlooked when there is no console? Or anything...
There's nothing special about running code in an executable jar file. If you run it from a console, e.g. with java -jar foo.jar the output will still go to the console.
If you run the code in some way that doesn't attach a console - such as javaw on Windows, which is the default program associated with executable jar files - then the output won't go anywhere. It won't cause any errors - the text will just be lost.
Note that in that case, if you use System.console() instead, that will return null. So:
System.out.printf("Foo%n"); // No problem. Goes nowhere.
System.console().printf("Foo%n"); // Would throw a NullPointerException.
The output is omitted, as long as you do not run your application from a console window (java -jar executable.jar). Furthermore, you can configure your Java installation such, that a console windows is launched as soon as you start a Java application. Output will be written to the JVM's console window then. You can find an article How do I enable and view the Java Console? on the official Java web site.
I you open it from the console (java -jar YourJar.jar) the text gets printed in your console window.
If you open it in the explorer (or similar), you won't see the text
To clarify your second Edit:
The bytecode is not omitted, because the compiler cannot know in what context the jar will be executed. The jar can always be called from console, in that case the println has to stay there.
In fact, many jar Files would be completely useless otherwise. There are plenty of Java programs that interact with the user by console in- and output.
It is not neccessary for a java-Program to have a GUI (or to run completely in the background).
Related
Tools: Win 7, Launch4J 3.5, Simple Hello world Java console app (bundled in a JAR file)
Hello all,
I have a basic JAVA console application that doesn't request any inputs, just a simple application that opens a console window and displays Hello World text.
I built it so simple so I can experiment with Launch4J 3.5 and build an executable file from the jar file.
Everything looks fine, the exe builds successfully but when I launch it nothing happens, I get the hour glass for a few seconds then nothing. I check the Task Manager and there's no process stuck in there.
See my settings in Launch4J, I only filled in the basics, I tried with and without an entry in the Wrapper manifest field:
Output file: C:\Development\SFDC\ProjectX\out\exe\ProjectX.exe
Jar: C:\Development\SFDC\ProjectX\out\artifacts\ProjectX_jar\ProjectX.jar
Wrapper manifest: C:\Launch4j\manifest\uac.exe.manifest (also tried with leaving this blank)
The rest is all left as default.
If by launching you mean double clicking it, no - nothing you can see will happen; you have to 'tell' Java to run your application with an associated console. To do this, you may create a new .bat file: Simply open a text editor and insert the following line:
java -jar NAME.jar
where "NAME" is the name of your application. Save the text file in .bat format, not in .txt, and place it in the same directory as your application. You can launch your application by double-clicking that file.
The reason it does not pop up in your task manager is because probably (I cannot know) your application only prints out a simple message and does nothing more. In non-console mode, it will just call your print (println or any other console) method without having any visual effect as there is no console to print the message to. In both cases however, if you only print something and do not perform other operations that 'last', such as listening for input, your programm will terminate as it has reached the end of the main method.
Yes. You do need to use the console mode.
You also do need to have some method of keeping the console window open, because it closes the console the moment the program terminates. Use scan.nextLine(); or Thread.sleep(i) if you really need to.
Possibly you need to use launch4j in console mode, see this answer: lauch4j hello world program
I have not been able to find a clear answer on Google.
What happens if an unhandled exception is thrown during runtime by an executable jar file? Will it open CMD? Also, would (exception_name).printStackTrace() do the same?
I would test these things, but I do not know how to create an executable .jar. So a short explanation on how to create one in Eclipse would also be appreciated.
(Easy to lookup and found)
What happened when you put together an example jar and tried yourself?
They'll go to the standard error output. If you don't set it programatically, then it depends on how you're running the jar.
From an IDE it goes to your IDE's output.
If you run the jar from the command line, then that command line is your standard out.
If you run the jar by double-clicking it, then the standard output isn't shown anywhere. It won't automatically open a command prompt, which you would have seen if you just created an example program.
See also, possible duplicate: Java jar output. where it goes?
Edit: Another possible duplicate: Where is System.err on Windows?
When you run a file, it opens the console window and all the way on top it has the directory of where the file is. This is pretty irritating because now, to separate other lines from mixing with the directory, I have to use "\n" on before any System.out.println() commands can be initiated.
"C:\Program FIles..." I want to get rid of that.
There is presently no way to disable this.
It does show useful information which shows which jvm is being used and which parameters are used to configure it, so it does "annotate" your program log to some extent. You'll also get a line stating the exit code when your program terminates.
There is an option to be able to write the console log to a file, I'm not sure if it shows the jvm version and exit code or not, perhaps you can try it and see if it is be useful to you.
Maybe try Settings->Editor->General->Console, in the "Fold console lines that contain:", add a substring of the line you want folded e.g. "C:\Program Files
So,
I have made a little game in eclipse, it generates a rondom number from 1 - 1000 , when playing out of Eclipse, it plays in the eclipse console, but what if i want to play it without eclipse?
There is no console showing up?
Anyone a idea on how to fix this?
EDIT : I would like a new GUI instead of CMD.
Grts
PS: Tell me if you need a specific part of the source code
1) You should probably export your program as a .jar file.
This isn't absolutely necessary - but it's better. And it sounds like you've already done it :)
2) Be sure to specify the main class in Eclipse before you export the .jar
3) Once you have the .jar, you can:
a) execute "java -jar myjar.jar" from a command prompt (for a console-mode program)
b) execute "javaw2 -jar myjar.jar" from a command prompt (for a Swing/GUI program)
4) You can also set a "file association" with javaw in Windows so that you can just double-click on the .jar to execute it:
Running JAR file on Windows
It seems the OP wants to reroute console output to a location of his choosing, not open an actual shell.
Here is one way to reroute stdout.
You can send it to a string, stream, file, etc. and then print it yourself into a window.
If you want a GUI, you will have to develop one yourself. There is no "Java Console GUI" that I've ever heard of.
I encountered this problem several times. If I pack a Java application in an executable jar file that takes arguments from a user, then the user have to invoke the program (jar file) from the command prompt by the following command:
java -jar "jar-file-name.jar"
But I want that whenever a user double clicks on the executable jar file (that needs arguments from the user), a window (command-prompt window) appears that would appear if we had invoked the jar file from the command-prompt.
I know one solution to this is using batch file .bat to run the jar file. Is there any other solution?
To explain why:
There are 2 java JVM exe launchers:
java.exe: console based - provides console input/output.
javaw.exe: for GUI apps - hides the console.
JAR file extensions are associated with javaw.exe by default, which is why you don't get a console when you double-click them.
The answers others have given, and adding my own:
rewrite your app so that it uses Java GUI items for input and output instead of System.in/System.out. This may be over-complex for what you require.
You mentioned creating a batch file so that the console-based Java JVM (java.exe) is run, You could also create a windows shortcut specifying the command line: java -jar jar-file-name.jar
You could change the windows file associations for .jar (but generally this is a bad idea -- new Java installs may reset this, and it will mean all java apps run from jars will have a console)
You could use a Java launcher like WinRun4J which allows you to simply drop a double-clickable EXE with an icon and a config file that specifies how your app should be run (with/without console, and with any other JVM and command line parameters )
Personally I went for the last option in my project - I made my jar file non-executable, and the user has to double-click the EXE. It also allowed me to specify a nice icon for my project, and provide multiple options on launch (debug/non-debug mode) simply by having a different exe/config file.
You would need to reassign the explorer association for ".jar" to java.exe instead of javaw.exe. This is somewhat of a questionable thing to do - it might make more sense to create your console output window. You can, of course, trivially pop up a dialog for user input of required execution parameters if they are not supplied on the command line.
There is working code for a console output JTextArea here.
This is an operating system feature.
If you want full control, then you must provide this functionality inside your program (or with a wrapper class.