包 java.nio.file 不存在 [英] package java.nio.file does not exist
问题描述
我目前正在研究如何从命令行编译 java.这是我所拥有的:
这是我所拥有的:
<前>/myjava/compile.cmd/myjava/src/a_pack/HelloWorld.java/myjava/src/b_pack/Inner.java/myjava/src/b_pack/Inner2.java/myjava/bin你好世界:
<前>包 a_pack;导入 b_pack.Inner;导入 b_back.Inner2;导入 java.util.ArrayList;导入 java.util.Iterator;公共类 HelloWorld {公共静态无效主(字符串 [] args){System.out.println("你好,世界");内部 myInner = new Inner();myInner.myInner();Inner2 myInner2 = new Inner2();myInner2.myInner();ArrayList myArray = new ArrayList();myArray.add(1);myArray.add(2);myArray.add(3);迭代器 itr = myArray.iterator();而 (itr.hasNext()){System.out.println(itr.next());}}}内部.java
<前>包 b_pack;公共类内部{公共无效 myInner() {System.out.println("内部方法");}}Inner2.java
<前>包 b_pack;公共类 Inner2 {公共无效 myInner() {System.out.println("SecondInner");}}我正在用 javac -d bin -sourcepath -src src/a_pack/HelloWorld.java
编译这个这很好用.
现在我的理解是,因为 HelloWorld.java 在它的 import 语句中引用了其他包,然后 javac 去编译它们.我猜对于所有的 java 包,javac 在内部都有它们.
无论如何——如果我将以下导入行添加到 HelloWorld.java
import java.nio.file.Files
;
它失败了
<前>D:.....myjava>javac -d bin -sourcepath src src/a_pack/HelloWo文件srca_packHelloWorld.java:8: 包 java.nio.file 不存在导入 java.nio.file.Files;^1 错误这里有什么故事?为什么有些java包好有些不好?
Java NIO 是在 Java 7 中引入的.来自 JDK 早期版本的编译器将阻止任何包含这些 NIO 类的代码.您需要升级到 JDK 7 或更高版本.
I'm working out how to compile java from command line at the moment. Here's what I've got:
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
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.
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.
Anyway - if I add the following import line to HelloWorld.java
import java.nio.file.Files
;
it fails with
D:.....myjava>javac -d bin -sourcepath src src/a_pack/HelloWo rld.java srca_packHelloWorld.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 NIO was introduced in Java 7. Compilers from earlier versions of the JDK will baulk at any code that contains these NIO classes. You need to upgrade to JDK 7 or higher.
这篇关于包 java.nio.file 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!