[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Cron Job

Posted on 2006-03-25
26
Medium Priority
?
2,814 Views
Last Modified: 2008-01-09
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
Comment
Question by:paddyfitz2005
  • 7
  • 7
  • 6
  • +2
25 Comments
 
LVL 10

Expert Comment

by:radarsh
ID: 16289005
>> 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
 

Author Comment

by:paddyfitz2005
ID: 16289024
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
 
LVL 11

Expert Comment

by:x4u
ID: 16289033
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:x4u
ID: 16289059
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 16289799
>>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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 16291009
> 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
 

Author Comment

by:paddyfitz2005
ID: 16291134
>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
 
LVL 11

Expert Comment

by:x4u
ID: 16291263
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
 
LVL 92

Expert Comment

by:objects
ID: 16291270
and make sue you make it executable
0
 

Author Comment

by:paddyfitz2005
ID: 16291283
>>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
 
LVL 92

Expert Comment

by:objects
ID: 16291304
javac g:\Java\Tomcat 5.5\webapps\DBTest\Updater.java
0
 

Author Comment

by:paddyfitz2005
ID: 16291321
>>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
 
LVL 92

Expert Comment

by:objects
ID: 16291340
g:
cd \Java\Tomcat 5.5\webapps\DBTest
javac Updater.java
0
 
LVL 11

Assisted Solution

by:x4u
x4u earned 1000 total points
ID: 16291567
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16293468
>>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
 

Author Comment

by:paddyfitz2005
ID: 16293478
sorry didn't see that, how can i resplit the points?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16293641
You can just reopen it - i'll do it for you, so you need do nothing at the moment
0
 

Author Comment

by:paddyfitz2005
ID: 16293653
how do i reopen it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16293663
Don't worry - you'll get a notification telling you it's reopened
0
 
LVL 11

Expert Comment

by:x4u
ID: 16294415
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16294449
>>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
 

Author Comment

by:paddyfitz2005
ID: 16294559
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16294574
Never mind paddyfitz2005. I missed that earlier comment about the command, although no-one seems to have said much about scripts ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16294578
You beat me to it - thanks anyway
0
 
LVL 11

Expert Comment

by:x4u
ID: 16295042
Don't worry paddyfitz2005, it's just a game. ;-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question