简体   繁体   English

任务计划程序无法启动Bat文件

[英]Task Scheduler Does Not Start Bat File

My bat file is: 我的蝙蝠文件是:

 @echo off 
 java -cp * MyTimerTasker

I try to run main function in MyTimerTaskerClass. 我尝试在MyTimerTaskerClass中运行主要功能。 All jars and bat file are in same folder. 所有的jar和bat文件都在同一文件夹中。 When I try to run bat file with double click, it runs. 当我尝试双击运行bat文件时,它将运行。 When I try to run with right click and run as administrator, command window shows and disappears but my main function does not start. 当我尝试右键单击并以管理员身份运行时,命令窗口显示并消失,但我的主要功能没有启动。 When I try to run with task scheduler, it never starts. 当我尝试使用任务计划程序运行时,它永远不会启动。

Edit: My main class. 编辑:我的主班。

 public class MyTimerTasker {
 public static void main(String[] args) throws IOException {
    FTPDownloadFiles ftpDownloadFiles = new FTPDownloadFiles();
    System.out.println("Running ...");
    DatabaseTask databaseTask = new DatabaseTask();
    databaseTask.connectToDatabase();
    ftpDownloadFiles.downloadFiles();

    try {
        databaseTask.parseFiles(JdbcConnection.filesPath);
    } catch (SQLException e) {
        e.printStackTrace();  
    } catch (Exception e) {
        e.printStackTrace();  
    }
    finally {
        try {
            databaseTask.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();  
        }

    }

}
}

You are adding the classpath of all the files in the working directory. 您将在工作目录中添加所有文件的类路径。 You should add the name of the jar to the classpath. 您应该将jar的名称添加到类路径。

If you start as administrator, it starts in another directory (same if you start as a scheduled task). 如果以管理员身份启动,它将在另一个目录中启动(与作为计划任务启动的目录相同)。 You will have to set your working directory in the batchfile: 您将必须在批处理文件中设置工作目录:

cd /d "%~dp0"

This will change the working directory to the folder, where your batchfile resides. 这会将工作目录更改为批处理文件所在的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM