Windows Task Scheduler Program Ends Immediately - java

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.

Related

Make a jar file run every hour automatically

I am very new in developing world so my apologies in advance if my question sounds weird.
I have written a test with Selenium and JAVA and made a jar file from it, now I am wondering if there is any way that this jar file can be run every 1 hour automatically, I mean there should be no one clicking on the jar file or any running program to run it.
Open Task Scheduler and Create Task (on right side).
Add a Name on the General tab.
On the Triggers tab, click New...
In the popup, select daily
then in the advanced you can select Repeat Task every:
1 hour
On the Actions tab, click New...
Browse and select your program
Click OK to create the task.
You can use an OS built job scheduler or use a tool which would make your jobs run.Also you could have a main Java program running infinitely and a child process spawning every 60*60*1000 milliseconds .
You can also run Jenkins, and create a job to execute the jar every hour. The nice part about this is you get a web-based UI and an easy way to view the output from the tests.
If you are on a unix based system, you could run the jar as a cronjob. The following would run a jar every 30 seconds.
0 */1 * * * java -jar /path/to/jar/myjar.jar
Read the following to learn how to setup a cronjob correctly https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
For windows, use task scheduler. Go to https://stackoverflow.com/a/26932169/802061 for more details.

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

starting and stopping a jar from jenkins

I've been working on my jenkins server off and on for the last few weeks. Right now it runs fine building and outputting the files to a remote location. However, i'm looking to do some pass or fail tests on the files before they are uploaded. This way i avoid releasing broken versions of my work. As well avoid more bug reports that i do not need to be reading.
The files i'm looking to run are actually plugins for another program. So i need to actually start this program as i can configure all the files before hand. The program is a .jar file which i know i can launch using a bat of shell script. The issue is i don't know at this point how to terminate the .jar program after it has been running. All the solutions i've found require me to modify the jar to terminate itself, or kill the JVM. Both i can't due for varies reasons.
It's a heavy hack, but you can make a shell script start a background thread that checks if the JVM is done, then terminate it.
Rudimentary example in bash:
start_jvm_command >>console.log 2>&1 </dev/null &
while ! grep 'done signal' console.log; do sleep 1; done
pkill -f "regexp for start_jvm_command"
Note: I was using pkill to terminate to avoid wrapper shell scripts.
It been a year since i asked this question but i found a much nicer way to solve this question. Its called JUnit and is a testing lib for java. With it i don't need to launch the entire jar but can test peace by peace at a time. As well if require i can write a test to launch the entire .jar file. In the test i can check if files exist, if functions complete correctly, and if any error is thrown during runtime.

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

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.

Can my Java software be transformed to .exe software?

Without learning new programing languages, can we using Java get .exe (executable windows file) software directly? And is there away to make .jar (Java ARchive) software transform to.exe (executable windows file)?
Would this conversion effect the performance of the software?
One of the important points of Java is that it'll run on any platform (e.g. Windows, Linux, etc) that has a suitable Java Virtual Machine (JVM) installed. A .exe file is compiled to work on only one platform.
What you think you want is to turn a .jar into an .exe so you can launch it easily. What I expect you really want is just an easy way of launching a Java app that anybody can understand, including your parents.
The easiest way of doing this is to create a Windows batch file that launches your Java app. One of line of script, one new file, one new instruction to your users - "Double click on runme.bat"
There are ways of turning a .jar into an .exe, but you should think about whether that's really what you want, and if so, why.
Note: you can launch a .jar in Windows by just double clicking on it, as long as the main class is specified in the manifest. You might want to look into tools like Apache Ant to simplify building .jars and their manifests. Just a thought.
EDIT:
A batch file in Windows is a simple text file that contains commands. You can test the commands by running them in the command prompt (Start -> Run -> cmd). When you run a batch file the commands in it are just fed to the command prompt one at a time.
How to run a Jar from the command prompt: "java -jar myfile.jar"
If you create a batch file (.bat - use Notepad or your favourite text editor) containing "java -jar myfile.jar" (without the quotes!) then double-clicking it will launch the main class specified in the manifest of myfile.jar. myfile.jar has to be in the same folder as the batch file, though.
If you use the command "java -jar lib\myfile.jar" (again, without the quotes) then your batch file will run myfile.jar, which needs to be in a folder called lib that's in the same folder as the batch file. Use this approach when you have a whole load of Jars with your application and you don't want to shove them in the user's face :)
Note that moving the batch file will break it, unless it uses absolute paths to the jar file - e.g. "java -jar C:\dev\myfile.jar". Of course, if you use absolute paths then moving the Jar will break the batch file anyway :)
Also, note that you should be able to run a Jar file just by doubling clicking on it in Windows, as long as the main class is specified in the manifest. Give it a try. Of course, convincing your users that they can double click on it is another matter entirely...
As a final note, if you use a batch file, your users will get a nice command prompt window sitting in the background until your Java app closes. That is, unless you start your batch file command with "start". E.g. "start java -jar myfile.jar". If you have your app configured to log to System.out or System.err, you will still get a command prompt when your app writes to either of those streams, though.
Final final note, in the Linux world the equivalent of batch files are shell scripts.
You can launch a .exe from java with Runtime.exec() or ProcessBuilder.
You can make a .exe launcher with http://launch4j.sourceforge.net/
Yes, it is possible: http://www.excelsior-usa.com/articles/java-to-exe.html
I'm not sure if this is an appropriate answer because I don't work with Java but could you not using a shortcut file pointing to the java runtime and pass the jar as an argument, this
I realise this is not the only way, nor probably the best way but to me it seams a little better than creating a batch or converting to an exe file if all you're looking for is a convienient way to launch a java app
The very simplest way is to write a windows batch (.bat) file which will start the application.
I was using a wrapper for java to make exe files for a pretty long time:
JStart32
It is just a wrapper for *.jar files.
But ask yourself:
Wouldn't an exe kill the purpose of javas platform independency?
Check out jsmooth (free) and exe4j and you might want to read make your swing app go native

Categories