.jar File Not Working With Windows Scheduler - java

Hi all so i have wrote a program in java (using eclipse) and exported single class program to a .jar file. This program also starts a batch file. When i double-click the .jar file the jar runs perfectly and starts the batch file.
But what i want to do is for the .jar file to run every weekly, so with windows scheduler i created a task with the action being the .jar file. This did not work. I then read somewhere that windows scheduler dose not like .jar so i thought of making a second batch file(start.bat) to start the .jar which would then start the first batch file.
The command in my start.bat is
java -jar myJar.jar
When i double click the start.bat file everything works. But when i set the windows scheduler to start this task i get the following error message for a cmd window
Error: Unable to access jarfile myJar.jar
This really has me stumped as all the files are in the same directory.
Any help would be seriously be appreciated, thanks.

Obviously this comment was the answer:
use the full path of myJar.jar instead of a relative path - the running directory of the windows scheduler is C:\Windows\System32 and your jar-file is probably not in this directory.

Task Scheduler cannot run .jar directly you need to run it via command prompt.
Because the task scheduler runs the .bat via cmd its default executing location i.e, C:/windows/system32 we need to change the path.
While scheduling task in scheduler call TaskName.bat as action.
SO,
Create a batch file "TaskName.bat" In TaskName.bat
Enter the Following
#echo off
cd "Path to the jar file Example C:\MyFolder"
java -jar Nameofthejar.jar
pause

If you follow the following steps you will not get any issues.
Step 0 : Setup
Add app.schedule.externally_managed=true in application.properties
Step 1: Create a New Task
Click Create
Provide details
Configure for Windows 10 is important
Step 2: Trigger Details
Step 3: Action Details
Step 4: Actions
Make sure all the checkboxes are unchecked as shown below, this is important
Step 5: View Task Details
Refer this and this for more details

Related

Why the BAT file launched from JMeter OS Sampler is not triggering the Maven execution?

Summary:- I need lots of dynamic data for my performance testing and it's not possible to generate those test data from Jmeter itself. Hence, I wrote a Java code which will generate these dynamic test data and will put those data into the excel file. This excel file can be consumed by JMeter script for the performance testing. Every iteration in JMeter needs a new set of test data and that's why I have created a bat file which will trigger the Maven execution(it's just mvn clean test) and will generate the fresh set of test data before each of iteration. Everything is working fine till this point. I just need to run the bat file from JMeter to trigger the test data creation before each iteration and that's the problem which I am facing
Problem:- As mentioned in the link How to run batch file(.bat) from Jmeter and as suggested by user #Dmitry T, I have added the OS sampler with the given parameters(See the screenshot below) but it is not starting the Maven execution. It is hitting the bat file(I put some msg command to check) but somehow it is not starting the execution. I tried the other solution given by the same user about using the Beanshell Sampler and running the command
Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /c D:/XXXX/XXX/XXXX/GenerateTestData.bat");
This is also not working. Am I missing something here? Please let me know if there is any solution for this? Appreciate any help on this?
The batch file is most likely not designed to work properly with current directory on execution being different to the directory containing the batch file. The current directory can be any directory. Very common are the directories %SystemRoot% (Windows directory) and %SystemRoot%\System32 or %SystemRoot%\SysWOW64 (Windows system directory) as current directory, whereby any directory can be the current directory on running a batch file.
A batch file referencing other files or directories relative to the batch file directory should set the current directory to the batch file directory or reference all directories and files with full batch file path.
The argument 0 of a batch file is always the batch file itself. The help output on running in command prompt window call /? explains how to reference an argument with a modifier. In this case %~dp0 should be used to get full path of the batch file.
So in the batch file can be used at top:
#echo off
cd /D "%~dp0"
The current directory is set with second command line to the directory containing the batch file as long as the batch file is stored on a drive with a drive letter.
There is another method to make the directory of the batch file the current directory which works even with batch file being stored on a network resource and the batch file is executed using its UNC path.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
rem Other commands accessing files and directories in batch file directory
rem using no path or a path relative to current working directory.
popd
endlocal
The help output on running in a command prompt window pushd /? describes why this code works even with a UNC path on command extensions enabled which is made sure by the second command line which defines together with first command line completely the execution environment for the batch file without depending on configurations outside of the batch file.
Another solution is referencing all files and directories in batch file directory with full path which means with using %~dp0, for example "%~dp0ExcelFile.xlsx".
Note: The path string referenced with %~dp0 always ends with a backslash which is the directory separator on Windows as explained by Microsoft documentation about Naming Files, Paths, and Namespaces. Therefore concatenation of %~dp0 with another string like file/folder name or wildcard pattern should be done always without using an additional backslash for a 100% correct full file/folder/pattern argument string.
In the Command input provide full path to the cmd.exe
Change the Working directory to where your batch file lives
Use just batch file name in the Command Parameters
Something like:
See How to Run External Commands and Programs Locally and Remotely from JMeter article for more details.
Alternatively you can use Maven Exec Plugin to run your custom command before running the JMeter test

Running a batch file as a scheduled task does not behave as it should?

I have a jar file, which uses jdbc to connect to a sql db and add a new record. To execute this with an arg I put the cmd line command in a batch file to run it.
Now if I manually click and run the batch file it works fine and I can see the new record in my sql database, but I have created a scheduled task to run the batch file for me once day which it seems do successfully but there are no new records added to the database.
Any ideas on why this happens?
The batch file is just one line, as it seemed to work when I ran it? :
java -Dvar=daily -jar SQL_JDBC_Bandon.jar
I post this as an answer because it would look ugly in a comment.
To have a log of executions to compare to the task execution log of scheduled tasks create a batch similar to this.
#echo off
echo JDBC executed %DATE% %TIME% >> C:\temp\logs\jdbctask.log
C:
cd \PathToYourJar
C:\Programs\Java\bin\java -version >> C:\temp\logs\jdbctask.log
Dir C:\Programs\Java\bin\java.* >> C:\temp\logs\jdbctask.log
C:\Programs\Java\bin\java -Dvar=daily -jar SQL_JDBC_Bandon.jar >> C:\temp\logs\jdbctask.log
This makes sure that you don't rely on PATH or JAVA_HOME variables that might not be set for the user you run the batch in. Please adjust the paths to your needs.
If this still fails, post the results of the log file.
I figured it out a while back, but all I did was put these 2 lines in a .bat file and it works without any issues as a scheduled task:
cd C:\Users\SQLService\Desktop\ScadaCalcs
java -Dvar=daily -jar SQL_JDBC_Bandon.jar
One thing to note is that the jar and script are both in the ScadaCalcs directory, but I had to cd an absolute path for it to work.

Run Java Application at Windows Startup

How can I make java application start with Windows, without needing user interaction in netbeans ??
Follow the below steps:
Create a *.jar file using Netbeans
Create a batch file with the following contents:
javaw.exe -jar path/to/file/your_jar_file.jar
Create a basic task in the Windows Task Scheduler. Select "When I log on" as the trigger. Set the action to starting a program, and choose your batch file as the program.

Using task scheduler for running java jar (from batch file)

I am scheduling a task (with windows task scheduler) which simply run a batch file.
this batch is running a jar file (Java program) with a simple "Java -jar test.jar".
When i run the script from the command line manually, the java program runs well and no exception is shown.
but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar.
What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?
Thanks.
I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?
Does your jar have any dependencies? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line.
Anyways, depending on your case, you can do something along these lines:
cd /path/to/exec/folder // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]
This has to work, if you specify everything you need correctly.

Running a JAVA program as a scheduled task

I am trying to run a simple JAVA program once per day on a Windows 7 machine.
My code runs fine inside NetBeans. If I do a clean and build it suggests this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This does not work from the DOS prompt of course because of the space between program and files so I do this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This works from the DOS prompt.
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
with arguments:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
When I then run it, all I see is a DOS box flashing up for a second. I expect the code to take about 30 secs to run. The code should persist data to a database and no updates happen.
The code also uses java.util.logging so I should see log entries and I don't.
I strongly suspect that I am not running the JAVA command properly or that there's a bad classpath issue that it present when running via Scheduler that isn't there when running from the DOS prompt.
Help would be appreciated. If you've seen this before and can sort it that would be great. If you can tell me how to get a meaningful error trace from Scheduler than that would also be really helpful.
Thanks!
I Think that you could create a simple batch script that will launch your program in this way :
#echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
I solved it after changing all fonts' references to "SansSerif"
I was using Jasper Reports inside Java to create a PDF file. It was working fine when I double click the batch file or Scheduler with Windows Server 2003 but not working with the Scheduler of 2008.
I tried many different things nothing worked so I though Could it be that Windows Server 2008 is blocking the access?.
Now is working perfect. So, if you are having problems check the references to anything you are using.
The scheduler will run under a different user unless you specify what user to run as. If it isn't running as your user then it won't be able to write to your directories.
The real problem to the original question is a java installation issue on Microsoft systems. Java jre installs into Program Files\java. The executable (java.exe) is only installed in that java\bin directory. Running from the command line, the os looks in the proper location for the java.exe. Running from other MS tools (such as VBA Excel or in this case TaskScheduler), it does not!
You can see that TaskScheduler is looking in the wrong place by viewing the tasks history in the TaskScheduler tool. Double click on some of the history events and one will list the action and return code. The action will show that the TaskScheduler is trying to run
"C:\Windows\system32\java.EXE"
So, copy java.exe from the java\bin directory into the place where the scheduler is looking, and now it will work.
Or update your task and provide the full path to java.exe.
You can also update the environment system path to look for java in the java\bin directory, but that has to apply to all users and sometimes this is faulty as well.

Categories

Resources