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.
Related
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
Suppose I made a program to take two input and display the sum.
It runs in netbeans but when I build out the jar file , and
double clicked the jar file , nothing happens.
How can I make these program run?
You must run this from command line, and then execute java -jar {PATH_TO_JAR}
You can use the command-line :
java -jar <yourjar> <yourMainClass>
You can also add the main class in a manifest file into the jar. Doing so, you don't need to specify anymore on command line
Jar files needs to run in JVM - Java Virtual machine, to run your file use:
"java -jar" and your jar.
By doing so, you tell Java to run your jar in jvm which means it now can run.
I'm hoping someone can tell me how to run a jar file (located on Desktop) multiple times with one command? Ideally without a new terminal window opening for every instance as well. A bash script would be sufficient too, whatever is easiest for yourself.
Currently I'm just running java -jar filename.jar for each new instance but it gets repetative when I need to have 50 or so instances open.
Try this:
for i in {1..50}
do
java -jar filename.jar
done
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'm trying to run a jar file from Java code, through exec().
The jar I'm executing have some resources relative to its path that need to be loaded. So for example executing from console:
java -jar [/path/to/jar/]exec.jar
is working only if the command is launched from the same directory
I've tried with: cd /path/to/jar/exec.jar && java -jar /path/to/jar/exec.jar
but it seems there is a problem within exec() for running cd, widely covered on the web. The main problem is that I'm looking for a procedure that runs both on Linux and Windows.
I've tried to mess with the -classpath option, but with no luck.
Is there any simple solution to this? Note that I'm not "fond" of the system call idea, it's just I was looking for a simple way to schedule the execution of custom jars.
Thank you in advance!
CB
You need to set the working directory of the launched process using ProcessBuilder.directory(File).