This discussion is archived
5 Replies Latest reply: Oct 22, 2012 8:32 PM by 969840 RSS

jFileChooser() not working with windows task scheduler

969840 Newbie
Currently Being Moderated
I have a program that would send emails with attachments to users. The program should be ran according to the scheduled time, and because of that I use task scheduler to do it for me. I did all necessary steps to make this happen. (create .bat file to activate/run the jar file then task scheduler will trigger the .bat file if it hits the stated schedule.) The problem right now is that when the program is ran or is triggered by task scheduler, it gives off this error:
java.lang.NullPointerException
at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
at sun.awt.shell.Win32ShellFolder2$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I tried running it manually by using the .bat file and it works fine with no errors or whatsoever. I'm suspecting it has something to do with jFileChooser() because It wont retrieve the attachments in the default directory, but I'm not sure, there might be other causes. Here's the function that retrieves the attachments in the default directory:
 public String getMyDocument(String filename)
 {
     JFileChooser fr = new JFileChooser();
     FileSystemView fw = fr.getFileSystemView();
     return fw.getDefaultDirectory().toString()+"\\"+filename+".xls";
 }
PS.

All required attachments are present in the default directory, no misspelled words or misplaced characters. I also did some research but so far i haven't found any solutions yet.
  • 1. Re: jFileChooser() not working with windows task scheduler
    sabre150 Expert
    Currently Being Moderated
    What if the default directory when running from the .bat file is not the same as that obtained when using the task scheduler? Easy enough to check by printing it out .
  • 2. Re: jFileChooser() not working with windows task scheduler
    gimbal2 Guru
    Currently Being Moderated
    sabre150 wrote:
    What if the default directory when running from the .bat file is not the same as that obtained when using the task scheduler? Easy enough to check by printing it out .
    Depends on where the code exactly blows, the stacktrace gives no hint what the root call is. Methinks things already go wrong during the creation of the filechooser object, but I can only guess.

    Strange code anyway to use a filechooser to get a directory, especially when apparently no actual GUI is created. That seems like something that can easily change between Java releases.
  • 3. Re: jFileChooser() not working with windows task scheduler
    sabre150 Expert
    Currently Being Moderated
    gimbal2 wrote:
    sabre150 wrote:{quote
    Strange code anyway to use a filechooser to get a directory, especially when apparently no actual GUI is created.
    My immediate thought when I saw the code. I'm not even sure there is a default directory if the component is not realized but I have never checked. I would like to suggest to the OP that he uses the "user.home" directory but again I have never checked where that points when used with the task scheduler. It could be that it is not defined and that file chooser uses this as the default if not explicitly set. This is why I suggest that the OP writes out the value.
  • 4. Re: jFileChooser() not working with windows task scheduler
    baftos Expert
    Currently Being Moderated
    By the stack trace, I think the basic problem is that you use Swing components in a headless environment. Schduled tasks usually run under the Local System account.
    Otherwise, what others have said is fine with me.
  • 5. Re: jFileChooser() not working with windows task scheduler
    969840 Newbie
    Currently Being Moderated
    Report FILE: \\'ServerName'\'Department'\'Division'\Systems\'Group'\'User'\My Documents\Data Center (Warranty) Hardware .xls
    @sabre150 that's the directory that prints out when i use the .bat file. The problem is that the program won't even print the default directory when i ran it via task scheduler it already prints the error. I can't even determine if it searches for the wrong directory or not.


    The account being used is not local but i have already tried using the same account on other scheduled tasks and it works. I'm going to try to use a Local account hopefully that would work.

    Edited by: jaycroll on Oct 22, 2012 8:32 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points