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 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 NIO is a buffer oriented package. It means that the data can be written/read to/from a buffer which further processed using a channel. Here, the buffers act as a container for the data as it holds the primitive data types and provides an overview of the other NIO packages.
Java NIO enables you to do non-blocking IO. For instance, a thread can ask a channel to read data into a buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With