I have an executable JAR file on my desktop that I need to run once every three hours. I have no idea how to go about this. Please leave your suggestions/ideas.
You can use crontab (on Linux, Unix or MacOS) to run the jar at any time. To run the jar every 3 hrs, the cron expression would be
0 */3 * * * (java -jar ~/Documents/App/App.jar)
where ~/Documents/App/App.jar would be the location of the jar
For Windows OS, you may use schtasks. Check this link for the same : https://learn.microsoft.com/en-us/windows/desktop/taskschd/schtasks
You may also use the Task Scheduler of Windows to run the jar. Please check Run a jar file using windows scheduler for the same.
Probably, you can execute below command in command prompt to auto-trigger jar execution:
Schtasks /create /tn JarTask /tr C:\Users\abhinav\Desktop\MyApp.jar /sc hourly /mo 3
Related
So I've been writing this little code, and it works -- but I have to type in the whole Computer;~ User$ java MyProject and change the path every time, so I decided to stick it in a .jar file (so I could just run it on a double click). I am an Intellij user on running macos. I created the file, File/Project Structure/artifacts/+/myMain etc and now have a .jar file sitting on my desktop. When I run the jar inside of Intellij, or run the jar using
java -jar /Users/Me/Desktop/MyProject/out/artifacts/MyProject_jar/MyProject.jar, it works fine.
However, when I:
Run it from the terminal with java -jar MyProject.jar I get Error: Unable to access jarfile MyProject.jar
^^Resolved - using the chmod 755 MyProject.jar command, see comment for further details
Or run it on right click with Jar launcher I get The Java JAR file "MyProject.jar" could not be launched. Check the Console for possible error messages. There are no error messages in the console.
And before anyone says it, I have Java 13 and the project is running on Java 13 -- it's the default that my computer has, and has been updated automatically ever since installation.
How would I be able to launch this file, and what could be stopping it from doing so?
in terminal, do an
ls -l MyProject.jar
You'll see something like:
-rw-r--r-- 1 youruser yourgroup 1024 Apr 24 15:41 MyProject.jar
The -rw-r--r-- part is the file permissions where an "r" means readable, "w" means writable, and an "x" would mean executable, but as you can see there isn't one there. Without going into the long explanation, use the chmod command to make the file executable by doing
chmod 755 MyProject.jar
Afterwards, use the same ls command and you should see -rwxr-xr-x in the permissions field. The file is now executable (by anyone) and should launch if you click in in Finder.
If your program has no GUI but requires console input, you should run it from the terminal instead of double clicking it. When you double click the MyProject.jar file, you actually launch it through jar launcher. The jar launcher will pass the files to the JVM. However, since you are not launching it from the terminal, the JVM does not know which terminal to use, so the program might not be properly executed.
I'am trying to create a setup with innosetup for my application but before the finish of the installation of my setup.exe i'd like to run other installation by this command :
java -jar build.jar buildinfo.xml
this Jar installation will takes 45 seconds but i don't know how can i do it .
Also you can...
create a batch file with your commands ("java -jar" etc.)
use "Bat to exe converter" (search on Google) in order to convert
your batch file in a Windows executable file
run that exe file at the end of Inno setup procedure
I hope this can help someone :-)
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
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.
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.