繁体   English   中英

如何在Netbeans的运行配置中设置MONGOHQ_URL环境变量?

[英]How do I set the MONGOHQ_URL environment variable in the run configuration of Netbeans?

我们正在努力将Java项目部署到使用MongoDB的Heroku。 根据Heroku文档 ,数据库连接参数是从环境变量MONGOHQ_URL中读取的。 当我在笔记本电脑上的Netbeans中运行项目时,如何设置此变量?

我尝试在运行->设置项目配置->自定义->运行以及在操作->通过main()运行项目和运行文件中使用-DMONGOHQ_URL=...将其添加为VM选项。但无济于事。 程序使用System.getvar读取它时,未设置。

您可以在netbeans.conf文件中进行设置。 添加行:

export MONGOHQ_URL=...

这里有一个示例: http : //sunng.info/blog/2009/12/setting-environment-variables-for-netbeans/

好的,我知道了。 这对于Java程序员来说可能是显而易见的,但是我不是一个人,所以这就是我拼凑的东西。

    String mongo_url = System.getenv("MONGOHQ_URL");
    // If env var not set, try reading from Java "system properties"
    if (mongo_url == null) {
        mongo_url = System.getProperty("MONGOHQ_URL");
    }

    MongoURI mongoURI = new MongoURI(mongo_url);
    this.db = mongoURI.connectDB();

    // Only authenticate if username or password provided
    if (!"".equals(mongoURI.getUsername()) || mongoURI.getPassword().length > 0) {
        Boolean success = this.db.authenticate(mongoURI.getUsername(), mongoURI.getPassword());  

        if (!success) {
            System.out.println("MongoDB Authentication failed");
            return;
        }
    }
    this.my_collection = db.getCollection("my_collection");

暂无
暂无

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

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