[英]Why does this Java program compile on my computer with eclipse, but not any other computer with eclipse and JDK?
[英]Why does this simple Java example from 2008 fail in my up-to-date Eclipse/JDK?
这是 Java 中的一个非常简单的示例,旨在打印出所有系统环境变量:
http://www.devdaily.com/blog/post/java/java-how-to-print-system-env-environment-variables
代码看起来很简单 - 它只是将环境变量作为映射进行迭代,打印每个键和值,但是当我执行代码时出现以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from element type Object to String
这里发生了什么? 示例是垃圾还是我设置了 Eclipse / JDK 以防止其工作?
仅供参考,我在 Eclipse 的更新版本上使用 Windows XP 和 JDK 1.6.0_24 x86。
示例代码完全是错误的——它不能(也永远不能)编译,eclipse 应该在代码中显示出来。
问题是Map envMap
是原始类型,因此envMap.keySet()
也是原始类型,其元素在增强的 for 循环中无法隐式转换为String
。
解决方案:只需将 Map 定义更改为:
Map<String,String> envMap = System.getenv();
因为这个例子是错误的。 它应该包含该行
Map<String, String> envMap = System.getenv();
而不是
Map envMap = System.getenv();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.