• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2875
  • Last Modified:

Java Cron Job

Bascially i want to be able to run a cron job that will execute my java file updater.java every night at midnight.
My web server allows me to run cron jobs but i don't know how to compile the script to run the java file
I'm using windows and most tutorials on the subject are using linux
Please Help
0
paddyfitz2005
Asked:
paddyfitz2005
  • 7
  • 7
  • 6
  • +2
3 Solutions
 
radarshCommented:
>> i don't know how to compile the script to run the java file

What script do you want to run? How is it scheduled to run? In the script or
in the Java Application itself?

________
radarsh
0
 
paddyfitz2005Author Commented:
This is the information i got from my website provider:
-------------------

The Cron Job feature allows you to set Cron Jobs from the Web Console.
You cannot have more than 1 script starting at the same time.
The duration of the script may not be more than 1 hour.
Remember to make the script file as executable. Please refer to a Linux/Unix book
if not sure how to.

NOTE: You may also use the system command "crontab" to set Cron jobs.
If using the System cron you may specify the output to be mailed to
a specific email address with the MAILTO environment. The following
is a brief example of how to create a cron (for more details please refer
to a Linux/Unix book).
Login to your account via SSH/Telnet.
Create a file called mycron and add the following:

MAILTO="name@yourdomain.com"
15  23  *  *  *       /usr/ngasi/contexts/myaccount/myscript.sh

Save the file and enter the following command:
crontab mycron

The above cron job will run everyday at 11:15PM and execute
"myscript.sh". Any output will be emailed to "name@yourdomain.com".
--------------------------------------------------------

I just want to know how i can launch a java file  in a .sh script file
0
 
x4uCommented:
Download the Java SDK from sun and install it. ( http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=jdk-1.5.0_06-oth-JPR&SiteId=JSC&TransactionId=noreg )

Run the compiler with: C:\jdk1.5.0_06\bin\javac updater.java
(depends on where you installed the jdk)

Use a cron job like this: java -cp your_dir updater
(your_dir is the the directory where you compiled your Java file)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
x4uCommented:
The webserver is not running on your own maschine? Then you will have to compile it on your own maschine and upload the compiled .class files to the webserver. If only Java 1.4 is installed on the webserver you will need to use "C:\jdk1.5.0_06\bin\javac -target 1.4 updater.java" to compile the Java code.
0
 
CEHJCommented:
>>i don't know how to compile the script to run the java file

You don't compile scripts - scripts are text files. If you're on Windows, you need a batch file

@echo off
java -classpath <whatever> YourApp

will do it. Save the above (with appropriate alterations) in the batch file directory
0
 
objectsCommented:
> I just want to know how i can launch a java file  in a .sh script file

Same way you would run it from the command line
0
 
paddyfitz2005Author Commented:
>Use a cron job like this: java -cp your_dir updater


I tried to run it from my command prompt with the following: G:\> java -cp . Java\Tomcat 5.5\webapps\DBTest\Updater
but i get the following error ::  Error: Exception in thread "main" java.lang.NoClassDefFoundError: Java\Tomcat

but if i run it like this it works: G:\Java\Tomcat 5.5\webapps\DBTest>java -cp . Updater

-----------
So basically all a i need to do is put the corect command into a file called script.sh and I will be able to run it as a java file?
Thanks for your comments
0
 
x4uCommented:
Yes and you can also use it this way: java -cp G:\Java\Tomcat 5.5\webapps\DBTest Updater
This allows you to call it from every directory you like.

0
 
objectsCommented:
and make sue you make it executable
0
 
paddyfitz2005Author Commented:
>>you can also use it this way: java -cp G:\Java\Tomcat 5.5\webapps\DBTest Updater

Im still getting the same error when i do it this way?
Also when i try to compile it i get this error

G:\>javac Java\Tomcat 5.5\webapps\DBTest Updater.java
javac: invalid flag: Java\Tomcat
Usage: javac <options> <source files>


0
 
objectsCommented:
javac g:\Java\Tomcat 5.5\webapps\DBTest\Updater.java
0
 
paddyfitz2005Author Commented:
>>javac g:\Java\Tomcat 5.5\webapps\DBTest\Updater.java

This is strange i don't know what i'm doing wrong!

G:\>javac g:\Java\Tomcat 5.5\webapps\DBTest\Updater.java
javac: invalid flag: g:\Java\Tomcat
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are u
sed
  -classpath <path>          Specify where to find user class files
  -cp <path>                 Specify where to find user class files
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -d <directory>             Specify where to place generated class files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release

  -target <release>          Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
0
 
objectsCommented:
g:
cd \Java\Tomcat 5.5\webapps\DBTest
javac Updater.java
0
 
x4uCommented:
I didn't notice that you have a space in your path. You will need to put such pathnames in double quotes then. i.e.:

javac "g:\Java\Tomcat 5.5\webapps\DBTest\Updater.java"
java -cp "G:\Java\Tomcat 5.5\webapps\DBTest" Updater


0
 
CEHJCommented:
>>java -cp "G:\Java\Tomcat 5.5\webapps\DBTest" Updater

Mentioned that earlier:

>>
@echo off
java -classpath <whatever> YourApp
>>

(cp is the same as classpath)
0
 
paddyfitz2005Author Commented:
sorry didn't see that, how can i resplit the points?
0
 
CEHJCommented:
You can just reopen it - i'll do it for you, so you need do nothing at the moment
0
 
paddyfitz2005Author Commented:
how do i reopen it?
0
 
CEHJCommented:
Don't worry - you'll get a notification telling you it's reopened
0
 
x4uCommented:
>> Mentioned that earlier:
...

Didn't I write it even earlier? ;-) I just repeated it to show where to place the double quotes.
>>
Use a cron job like this: java -cp your_dir updater
>>
0
 
CEHJCommented:
>>Didn't I write it even earlier?

You did mention it earlier, but it would seem that i was the only one to mention scripts and how to use them
0
 
paddyfitz2005Author Commented:
This is the thing i hate about ee, theres always a dispute about who answered what and when. I'm going to split the points between the 3 guys and if you don't like it tough luck
0
 
CEHJCommented:
Never mind paddyfitz2005. I missed that earlier comment about the command, although no-one seems to have said much about scripts ;-)
0
 
CEHJCommented:
You beat me to it - thanks anyway
0
 
x4uCommented:
Don't worry paddyfitz2005, it's just a game. ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 7
  • 7
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now