gpt4 book ai didi

java - 在终端上设置环境变量,但 System.getenv 返回 null。 java问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:56 28 4
gpt4 key购买 nike

我更改了我的 ~/.bashrc 文件。我已经更改了 /etc/environment。我已经完成导出 WNHOME="/usr/local/WordNet-3.0"。我尝试了一切here和更多。 (我正在运行 arch linux,以防产生任何后果)。

我认为必须在我的计算机上设置环境变量,如果我使用 echo $WNHOME 检查它,我会得到正确的结果。

但是,当我在 java 程序中调用 System.out.println(System.getenv("WNHOME")); 时,我不断收到 null,这可能是什么原因?

输出如下所示:

Path is 'null/dict'
null
Exception in thread "main" java.io.IOException: Dictionary directory does not exist: null/dict
at edu.mit.jwi.data.FileProvider.open(FileProvider.java:306)
at edu.mit.jwi.DataSourceDictionary.open(DataSourceDictionary.java:92)
at edu.mit.jwi.CachingDictionary.open(CachingDictionary.java:133)
at MITJavaWordNetInterface.main(MITJavaWordNetInterface.java:30)

代码如下所示:

public static void main(String[] args) throws IOException
{
// construct the URL to the Wordnet dictionary directory
String wnhome = System.getenv("WNHOME");
String path = wnhome + File.separator + "dict";
System.out.println("Path is '" + path + "'");

URL url = new URL ("file", null , path );
System.out.println(System.getenv("WNHOME"));
//final URL url = Paths.get(wnhome, "dict").toUri().toURL();

// construct the dictionary object and open it
IDictionary dict = new Dictionary ( url ) ;
dict . open () ;

// look up first sense of the word "dog "
IIndexWord idxWord = dict . getIndexWord ("dog", POS . NOUN ) ;
IWordID wordID = idxWord . getWordIDs () . get (0) ;
IWord word = dict . getWord ( wordID ) ;
System . out . println ("Id = " + wordID ) ;
System . out . println (" Lemma = " + word . getLemma () ) ;
System . out . println (" Gloss = " + word . getSynset () . getGloss () ) ;
}

最佳答案

~/.profile文件中设置环境变量

如果我们在~/.bashrc中设置环境变量,那么这些变量只能由从shell启动的应用程序访问。为了让桌面应用程序访问环境变量,将其设置到 ~/.profile 文件中。

关于java - 在终端上设置环境变量,但 System.getenv 返回 null。 java问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210241/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com