首页 > 解决方案 > 在 IntelliJ 中将目录添加到类路径

问题描述

我正在尝试运行我的主类并将其他属性文件添加到类路径中,但它没有发生。我看到了一些解决方案,例如链接,但它们对我不起作用。我还尝试将“-cp \path to property files”添加到运行配置-> VM 选项中,但同样没有成功。

标签: javaintellij-ideaclasspath

解决方案


如果我对您的理解正确,如果您不想从现有文件中读取,则必须先创建文件,然后以类似如下的方式写入该文件:

    try {
        File f = new File("path/to/propertyFile.properties");
        if (file.createNewFile()){
            // some debugging
            System.out.println("File is created!");
        }else{
            System.out.println("File already exists.");
        }
        // you have your file now, and you can perform manipulations to it
        OutputStream output = new FileOutputStream("path/to/propertyFile.properties")
        Properties prop = new Properties();

        prop.setProperty("key", "value");
        // save properties to project root folder
        prop.store(output, null);

    } catch (IOException io) {
        io.printStackTrace();
    }

推荐阅读