簡體   English   中英

FileOutputStream在預定任務上不起作用

[英]FileOutputStream not work on scheduled task

我有一個jar文件,當雙擊它時可以正常工作,但是當我安排一個任務來運行它時,FileOutputStream將無法工作。

它可以正確執行其他任務,例如發送電子郵件並連接到路由器,但不能在文件上寫入。

我提取了最簡單的代碼來給出該錯誤:

package testjar;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestJar {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileOutputStream fout = new FileOutputStream("TestJar.log", true);
        fout.write("TestJar ok.".getBytes());
    }
}

我試圖通過調用運行該jar的.bat文件進行調度,並使用Launch4j從該jar中創建一個.exe:單擊該工具可以很好地完成所有工作,但是當我從計划任務中調用它時,它不會編寫文件。 (我正在使用Window7 Professional)

就像AnatolyG建議的一樣,它可以為日志指定完整路徑! (我想知道為什么..但是它有效,就足夠了!)

因此,這是使上面的代碼工作的示例:

public static void main(String[] args) throws FileNotFoundException, IOException, URISyntaxException {
    CodeSource codeSource = TestJar.class.getProtectionDomain().getCodeSource();
    File jarFile = new File(codeSource.getLocation().toURI().getPath());
    String jarDir = jarFile.getParentFile().getPath();
    FileOutputStream fout = new FileOutputStream(jarDir+"/JarTest.log", true);
    fout.write(jarDir.getBytes());
}

謝謝AnatolyG!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM