15

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!

Radu Murzea
  • 10,036
  • 9
  • 44
  • 68
Kevan
  • 861
  • 1
  • 5
  • 12
  • 1
    One way of debugging would be to put the full java -jar command in a batch script and execute that, redirecting any output to a file (see http://support.microsoft.com/kb/110930 ) – Dan Gravell Aug 20 '12 at 08:12
  • 1
    You're mixing slash (`/`) with backslash (\\). Are you sure that's ok ? – Radu Murzea Aug 20 '12 at 08:23
  • Are you using `System.getProperty("user.dir")` or `System.getProperty("java.class.path")` in your code? – Jonas Eicher Aug 20 '12 at 08:42

4 Answers4

17

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.

aleroot
  • 66,082
  • 27
  • 164
  • 205
  • I tried a number of things to get this working. I ensured that it was running as a user with enough privs. I fixed the / \ issue. Doing the above, running java indirectly via a bat file is what did the trick. Thanks aleroot – Kevan Aug 20 '12 at 19:42
  • Why would this work though ? Why executing a jar directly would fail ? – Ascendant Jun 09 '14 at 05:42
  • I was trying to schedule .bat file with no success for 3 days. This answer saved my life thanks @ aleroot – MajorGeek Sep 07 '15 at 15:21
1

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.

Luix
  • 19
  • 1
0

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.

Dan
  • 1,002
  • 5
  • 12
0

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"

TaskScheduler History Details

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.