2014-03-04 22 views
0

我有一个jar文件,双击它时会正常工作,但是当我安排任务来运行它时,FileOutputStream将不起作用。FileOutputStream无法在计划任务中工作

它正确地执行其他任务,如发送电子邮件和连接到路由器,但它不能写入文件。

我已经提取的给出错误的简单代码:通过调用运行的罐子,也从与罐子做的.exe .bat文件

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()); 
    } 
} 

我试图安排Launch4j:单击时它可以完成所有工作,但是当我从计划任务调用它时,它不会写入文件。 (我正在使用Window7专业版)

+0

你检查过用户(任务创建者)的权限吗? – Parth

+0

我试过它在不同的计算机上使用所有权限...没有任何变化。 – T30

+1

你看到一个异常,或者你只是没有看到文件夹中的“TestJar.log”文件?如果第二个,请确保您正在查看正确的文件夹,因为您没有指定“完整”文件名,并且“当前”文件夹取决于您如何运行应用程序。或者尝试指定文件的全名。 – AnatolyG

回答

0

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!