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?
Related
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).
When I created the program using JCreator on computer A, I can execute the program by double-clicking on the jar file, however when I brought the exact folder holding the jar file to a different computer without JCreator but with Java installed, a black screen that looked like command prompt appeared with one line of text instead of the opening JFrame. However, before I can read the line the command prompt looking screen disappears and ends the program. Can someone explain what is happening and how to fix this?
The jar is throwing an Exception before it can show any windows or take any action. To see the Exception it's throwing, run the jar via the command prompt instead of double-clicking it.
More info here: http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
Most likely you have a problem with the classpath of the jar looking for a resource that was being provided by JCreator but it can't find it, now that the directory has been moved. Use java -jar {jarfileName} to execute your jar file at the cmd prompt. You should then be able to see what is missing.
Hope this helps.
Hope there is a simple answer for this. We have an old SWING application that is launched from the command line. It depends on a set of libraries that are located in a folder C:\lib.
What is the correct syntax from loading C:\lib to the classpath. Something like this?
java -jar myapplicaiton.jar -CLASSPATH="C:\lib"
This seems to result in an Exception relating to being unable to see the libraries
I recently installed a java software in my PC and started writing some simple programs. I didn't face any problem while compiling the programs, but while executing it, it shows this error message -
"Windows can't open this file.
File: HelloWorld.java
To open this file, windows need to know what program you want to open it. Windows can go online to lookup it automatically, or you can manually select from a list of programs that are installed on your computer.
I know all the path settings are correct. In fact, there was no problem at all while compiling the program. What can be the problem of this? I even reinstalled JRE and that didn't help. Can someone help me?
Note: I'm using Windows 7 64 bit architecture OS and I'm using command prompt for compilation and execution of the file.
I'll assume that you've double-clicked the .java file, e.g. in the file explorer. A java source file isn't a (click-launchable) executable, and - without some acrobatics - neither is a compiled .class file: you shouldn't expect to double-click either and start your program.
In order to get this sort of behavior, you'll need to build a launchable program, and there are a few ways to do this. One is by making a batch file that runs the java VM with your code, another is creating an executable jar file.
To just run your code outside your IDE, you can invoke the java VM on the command line:
c:\> java HelloWorld
As to your specific error message, you haven't associated any program with .java files. Typically, as programmers, we want this Windows file association to be our editor of choice or our IDE. You can create this association by Right-clicking on the file, choosing Properties from the menu and then clicking the Change button beside Opens with: to pick an application.
But this is a side-issue: you still won't use this to make .java file executable. Search around this site for questions and answers about building executable jar files. If you're using a specific IDE like Eclipse or NetBeans, use that to refine your search.
When you want to execute the program, you specifiy the class name rather than the file name:
java HelloWorld
Don't use java HelloWorld.java, for instance.
I have created program that takes input from System.in and output to System.out. I create the executable jar and I want to run it without entering java -jar in windows. Is there any way to do this?
There are a number of ways to do this. The simplest way is to create a batch (.bat file) file that invokes it for you. There are also a number of solutions out there that will create a Windows executable from a jar file. Note: these programs don't compile the byte code, they just create an executable wrapper around the jar. One option is Jar2Exe but there are quite a few. JSmooth is another option I have used before that works quite well. Another advantage to a program like this is you can bundle all dependent jars into the same executable file as well. Some google searching for "jar to exe" should find you one that works for your solution.
You could write a .bat script that will start your jar.
On windows a simple double-click on the jar file should open it if the environment is well set.
You use the "start" command like "C:\> start myJarFile.jar".