Make a jar file run every hour automatically - java

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.

Related

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.

Creating a Shortcut for a Simple Java Application

I have a very simple java application that takes in strings and arranges them into lists based on the user's specifications. It has two files. Right now there's no GUI: I run it through the command line. Is it possible to compile it into a program that can be downloaded and run by clicking on an icon even without a GUI (a terminal window will pop up when you click on the icon) and have it still function? If so, how do I do so?
If you have Linux & a GNOME desktop, run 'gnome-desktop-item-edit' from terminal, set the command to whatever, and change 'application' to 'application in terminal'. In windows, create a .bat file that looks something like:
#echo off
java [-jar] <path to file>
And a terminal window will pop up that will exit when the command is done.
I don't know about others, but I'm sure there is a way.
Yes you can.
However, there will be some surprising behaviour.
First off, you can double click jar-files to run them. So what you want to do is to build a jar file.
I find that mkyong has a nice tutorial here
Now of course, that's how to do it manually. What you probably want in the long term is a way to do that automatically.
For long term (I am assuming you're a first year student or similar) use, I'd recommend learning Ant or Maven. These are tools that will compile your program, test them and if you tell them to, make jarfiles for you.
While I personally find Ant easier to get started with, but Maven is very widespread in the industry.
Now, let's say that you create the jar, you double click on it, and you enter your strings etc. and all is well... And then it all disappears.
This is because Windows will close terminal windows when they're done. So you'll have to remember to ask for an extra Enter keystroke or something before exiting.
This can be annoying but it's not insurmountable.

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

How to run a war file in cronetab

I have a Java project which gives me output based on user's request in the front end.
The output is like it will give me the list of devices from each and every server(based on click of a radio button).
Now I need to set up a crontab job which will run at a particular time and it will automatically get the output of all the servers without user's request.
Can anyone give me an approach as how I need to tune my program so that it will automatically give the list of all devices.?
Also do I have to create a war file and deploy that in crone tab to run the daily job?
WAR files are Web ARchives, meant to run inside a Web Container (think Tomcat, JBOSS, etc) - probably you want an executable JAR file. You are probably thinking of Java ARchive (JAR) files.
I would first create an executable JAR file and then schedule it as a CRON job to run at the frequency you wish.
How to create an executable JAR file
How to setup a CRON
job

Talend open studio - how to proceed with next step

Hi Im new to Talend Open Studio , My question is, Im able to create a Job and executing and getting output . Say for an example following is my Job , Now how can i proceed with the next step , That is how can i get the code or convert it into jar or any kind of executable to get my job done every time.
Above is my Job , And i want to automate this that is i need to run this for every hour or on the fly basis . How can i do with the further steps .
As of now i have created my Job how can i proceed with the next step
Thanks
The enterprise platforms of Talend come with Talend Administration Center that enables you to manage your jobs from a web interface and set up CRON jobs or other types of triggers. I'm not actually sure if this comes with the Talend Open Studio versions though.
An alternative is to export the job as a .jar/.war file and then have a scheduler or CRON job execute it every hour.
Right click on your job and there's an option called "Export Job" or "Build Job" (depending on the version you're using). This will generate the java code, and puts it into a zip. All you need to do is edit the zip, open it, and in the subfolder there will be a .bat and .sh file, its basically starting a JavaVM with parameters set, and starts your job.
You can schedule this bat/sh in windows/linux to run every hour also if you double click on it, you can execute on the fly.

Categories