8

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?

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • 2
    NIO was introduced in Java 7. What version of Java are using? – Reimeus May 16 '13 at 01:54
  • @Reimeus The JDK I'm using that contains javac is jdk1.6.0_32. – dwjohnston May 16 '13 at 02:52
  • 1
    There's your answer. You need to upgrade to JDK 7. – Reimeus May 16 '13 at 02:53
  • @Reimeus Just a clarification for JDK vs JRE. The javac I'm using belongs to JDK1.6.0, and I'm explicitly defining the location of the javac.exe. But the version of java (using `java -version` on my computer is 1.7.0_07. So is the reason it fails because packages aren't in the JDK? – dwjohnston May 16 '13 at 03:07
  • Yes, JDK1.6.0 won't do. You need the JDK 7 compiler. Your `java` is from another installation which is fine for running the application. – Reimeus May 16 '13 at 03:10
  • Ok, so when I use the JDK1.6.0 to compile my code without the nio package - why does it not compile the java packages (java.utils) into the bin folder? When I later run it, how does it know how or where to use them? – dwjohnston May 16 '13 at 03:17
  • BTW if you submit your first comment as answer, I'll accept it. – dwjohnston May 16 '13 at 03:18
  • Both the JDK & JRE 7 have the NIO classes in their runtime jar files. Java 6 JDK does not have these. If there's no Java 7 dependencies or syntax, then there's JDK 6 is fine... – Reimeus May 16 '13 at 03:21
  • Yup, so if I were to compile it with a JDK7 but run it in a JRE6 I'd get some kind of error? – dwjohnston May 16 '13 at 03:25
  • 2
    Only if you use any of the new classes or syntax introduced in Java 7... – Reimeus May 16 '13 at 03:27

4 Answers4

17

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.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

If you are on OSX, then check the JDK it is using...

$ 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.

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 post which saved me 5min of running dtrace.

dnozay
  • 23,846
  • 6
  • 82
  • 104
2

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.

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

Bryan Winstead
  • 49
  • 1
  • 11
1

I came across this issue and found my JAVA_HOME environment variable was still pointing to the old java 1.6.

  • Running javac -version showed 1.7
  • Running java -version showed 1.7

etc…

On removing that environment variable, things compiled fine.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
pcjose
  • 11
  • 1