默认目录必须是绝对目录

时间:2018-12-14 06:52:23

标签: java linux

我的代码调用log4j包中的方法:

org.apache.log4j.PropertyConfigurator.configure("log4j.properties")

在Windows系统和Linux虚拟机上运行没有问题。但是,在公司提供的Linux虚拟机上运行时,会发生以下错误,如下图所示:

enter image description here

我从哪里开始解决它?

补充: 这是我的代码:

public static void main(String[] args) {
    PropertyConfigurator.configure(configDir + File.separator + "log4j.properties");
    if(log.isDebugEnabled()) {
        log.debug("load complete:" + configDir + File.separator + "log4j.properties");
    }
}

执行的第一行报告一个错误,错误是:

Exception in thread "main" java.lang.ExceptionInInitializerError
...
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
    at com.dcits.provider.BaffleProvider.main(BaffleProvider.java:29)
Caused by: java.lang.RuntimeException: default directory must be absolute
...

2 个答案:

答案 0 :(得分:1)

当用户目录系统属性(即"user.dir")不是绝对路径时,不能构造默认的FileSystem。

在Windows和AIX上同时在Java 8中执行此代码会生成运行时异常,类似于原始发布者看到的那样。

public class RelativeUserDir
{
    public static void main(String[] args)
    {
        // Set the "user.dir" property to the "current directory" relative path
        System.setProperty("user.dir", ".");

        FileSystems.getDefault();
    }
}


Windows:

Exception in thread "main" java.lang.AssertionError: Default directory is not an absolute path
    at sun.nio.fs.WindowsFileSystem.<init>(WindowsFileSystem.java:61)
    at sun.nio.fs.WindowsFileSystemProvider.<init>(WindowsFileSystemProvider.java:53)
    at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:36)
    at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
    at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(FileSystems.java:89)
    at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:98)
    at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:96)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
    at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
    at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
    at RelativeUserDir.main(RelativeUserDir.java:12)


AIX:

Exception in thread "main" java.lang.ExceptionInInitializerError
        at java.lang.J9VMInternals.ensureError(J9VMInternals.java:146)
        at java.lang.J9VMInternals.recordInitializationFailure(J9VMInternals.java:135)
        at java.nio.file.FileSystems.getDefault(FileSystems.java:187)
        at RelativeUserDir.main(RelativeUserDir.java:10)
Caused by: java.lang.RuntimeException: default directory must be absolute
        at sun.nio.fs.UnixFileSystem.<init>(UnixFileSystem.java:67)
        at sun.nio.fs.AixFileSystem.<init>(AixFileSystem.java:55)
        at sun.nio.fs.AixFileSystemProvider.newFileSystem(AixFileSystemProvider.java:62)
        at sun.nio.fs.AixFileSystemProvider.newFileSystem(AixFileSystemProvider.java:55)
        at sun.nio.fs.UnixFileSystemProvider.<init>(UnixFileSystemProvider.java:68)
        at sun.nio.fs.AixFileSystemProvider.<init>(AixFileSystemProvider.java:57)
        at java.lang.J9VMInternals.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1773)
        at sun.nio.fs.DefaultFileSystemProvider.createProvider(DefaultFileSystemProvider.java:60)
        at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:78)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:119)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(FileSystems.java:100)
        at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:109)
        at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:107)
        at java.security.AccessController.doPrivileged(AccessController.java:638)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:107)
        at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:101)
        ... 2 more

答案 1 :(得分:0)

遇到此问题时,尤其是在jdk1.7下运行时,请注意是否已更改或设置了'user.dir'环境变量。我遇到的问题是'user.dir'是我错误设置的