包 java.nio.file 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16577797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
package java.nio.file does not exist
提问by dwjohnston
I'm working out how to compile java from command line at the moment. Here's what I've got:
我目前正在研究如何从命令行编译 java。这是我所拥有的:
Here's what I've got:
这是我所拥有的:
/myjava/compile.cmd /myjava/src/a_pack/HelloWorld.java /myjava/src/b_pack/Inner.java /myjava/src/b_pack/Inner2.java /myjava/bin
HelloWorld:
你好,世界:
package a_pack; import b_pack.Inner; import b_back.Inner2; import java.util.ArrayList; import java.util.Iterator; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); Inner myInner = new Inner(); myInner.myInner(); Inner2 myInner2 = new Inner2(); myInner2.myInner(); ArrayList myArray = new ArrayList(); myArray.add(1); myArray.add(2); myArray.add(3); Iterator itr = myArray.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
Inner.java
内部文件
package b_pack; public class Inner { public void myInner() { System.out.println("Inner Method"); } }
Inner2.java
内部2.java
package b_pack; public class Inner2 { public void myInner() { System.out.println("SecondInner"); } }
I'm compiling this with javac -d bin -sourcepath -src src/a_pack/HelloWorld.java
and this works fine.
我正在编译它,javac -d bin -sourcepath -src src/a_pack/HelloWorld.java
这很好用。
Now my understanding is, that because the HelloWorld.java references the other packages in it's import statements, then javac goes and compiles those. And I'm guessing that for all the java packages, javac has them internally or something.
现在我的理解是,因为 HelloWorld.java 在它的 import 语句中引用了其他包,然后 javac 去编译它们。我猜对于所有的 java 包,javac 在内部都有它们。
Anyway - if I add the following import line to HelloWorld.java
无论如何 - 如果我将以下导入行添加到 HelloWorld.java
import java.nio.file.Files
;
import java.nio.file.Files
;
it fails with
它失败了
D:\.....\myjava>javac -d bin -sourcepath src src/a_pack/HelloWo rld.java src\a_pack\HelloWorld.java:8: package java.nio.file does not exist import java.nio.file.Files; ^ 1 error
What's the story here? Why are some java packages good and some not?
这里有什么故事?为什么有些java包好有些不好?
采纳答案by Reimeus
回答by Bryan Winstead
The Files class consists of only static methods. I'm not sure if this is why it can't be imported, but it does mean it doesn't need to be imported.
Files 类仅包含静态方法。我不确定这是否是它无法导入的原因,但这确实意味着它不需要导入。
Edit: Just realized the package you specified is import java.nio.files.Files. The actual package is java.nio.file.Files; http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html
编辑:刚刚意识到您指定的包是导入 java.nio.files.Files。实际的包是 java.nio.file.Files; http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html
回答by pcjose
I came across this issue and found my JAVA_HOME
environment variable was still pointing to the old java 1.6.
我遇到了这个问题,发现我的JAVA_HOME
环境变量仍然指向旧的 java 1.6。
- Running
javac -version
showed 1.7 - Running
java -version
showed 1.7
- 运行
javac -version
显示 1.7 - 运行
java -version
显示 1.7
etc…
等等…
On removing that environment variable, things compiled fine.
在删除该环境变量时,编译正常。
回答by dnozay
If you are on OSX, then check the JDK it is using...
如果您使用的是 OSX,请检查它正在使用的 JDK...
$ cd /System/Library/Frameworks/JavaVM.framework/Versions
$ readlink /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
$ javac -version
javac 1.7.0_25
As you can see CurrentJDK
is pointing to the wrong version.
You can fix that by replacing the symlink.
如您所见CurrentJDK
,指向错误的版本。您可以通过替换符号链接来解决该问题。
cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo ln -fs /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents CurrentJDK
Credit goes to this blog postwhich saved me 5min of running dtrace
.
归功于这篇博客文章,它为我节省了 5 分钟的跑步时间dtrace
。