簡體   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