System.out.println not working with threads when executed from console - java

I have an application which uses threads to run heavy tasks. When the thread ends its own task, it creates a file and sends a message using System.out.println().
In Eclipse works fine, but once compiled into a .jar and called from console, it does not display anything. However it creates correctly the files.
What happends is, when i call the application via console, a new console prompt appears inmediatly, although the program is not finish yet (I know it because no file is being generated yet).

I suspect the problem is how you're executing the jar file. If you're just running it from Windows, it's probably being run with javaw.exe for example. Simply run it explicitly:
java -jar foo.jar
and all should be fine. This is unrelated to threads, by the way... it's just how jar files are executed.

Related

Why did a Java program keeps running even if I delete the executable .jar?

I would like to know why this happens and wether this will always continues to happen when I delete a .jar file.
This behavior would speed my updates to new .jars. I will be able to replace the .jar with a new one and restart the application right away.
I am always using linux to deploy my applications.
Linux does not completely delete the file as long as it is still used by a process. It will only disappear completely when the last user terminates or closes the file.

Windows Task Scheduler Program Ends Immediately

I have a Java executable (.exe) with a given JRE build in the same folder, which it uses to actually run.
I want to put this executable on Windows Task Scheduler.
I did some tests with some C++ hello world programs, and all went fine. This Java program, running directly (by two clicks or whatever) works all fine too (it is supposed to write to a file and end).
However, when I put the Java program in the Task Scheduler, it exits immediately, with status code 0x0 (success) and nothing is actually performed.
At Windows Task Manager, I see that javaw.exe starts and exits in a glimpse.
What could it be? Something related to Java? Something due to a specific task scheduler flag?
Aditional:
Java executable built with launch4j.
Scheduler set with schtasks /create /tn MyETL /sc hourly /mo 3 /tr C:\ETL\etl.exe
When you run an application with Windows Scheduler, if that application has dependencies to other files via relative path, then you need to set the start in setting for the task. This sets the path from where execution will begin.
Alternatively you can use a command file and have it navigate to the correct directory first.
Just figured out that the problem was that the program was actually being executed in the wrong folder, in order that the output file wasn't where I thought it would.
The output file was being write in the starting folder, not the program's folder.

What happens to "System.out.println()" in executable jar?

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).

Launch4J executable not executing as expected

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

why doesn't my jar file run outside netbeans?

I built a jar file that can run perfectly within netbeans when I click run,
but when I try to run the jar file by double clicking it it does not run, nothing happens..
Double-clicking the jar starts it, but unless you have a GUI application that opens a new window (in a different thread), it most likely finishes and closes before you can see anything.
In these cases you normally run the jar from the console (java -jar ..) to see if there are any exceptions/errors.
Starting an application from the command line would let you debug the problem as Bozho said. But you need to check if you have all the files in dist folder that you need to run your program. For example if you are using a database file to get the data, you need to place this file in the dist folder after program is built. I think the jar file is using a relative path when looking for files.

Categories