9

My java compiler cannot find the java.nio.file package. Consider:

import java.nio.file.*;
public class Test {
    public static void main(String[] args) { 
        Path currentRelativePath = Paths.get("");
    }
}

compiling with

bash$ javac Test.java

gives

Test.java:1: package java.nio.file does not exist
import java.nio.file.*;
                ^
Test.java:5: cannot find symbol
symbol  : class Path
location: class Test
    Path currentRelativePath = Paths.get("");
    ^
Test.java:5: cannot find symbol
symbol  : variable Paths
location: class Test
    Path currentRelativePath = Paths.get("");
                               ^
3 errors

I am using Ubuntu 12.04, and I think I have JDK 7 installed (see: package java.nio.file does not exist )

bash$ java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
Community
  • 1
  • 1
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174

6 Answers6

11

It is entirely possible that you have java 1.7, but javac 1.6 or even 1.5

maybe you can use

sudo update-alternatives --config javac

to configure it. If you cannot choose 1.7, then you need to upgrade your JDK package.

ljgw
  • 2,751
  • 1
  • 20
  • 39
  • Yes you are right.. I have for some reason javac version 1.6.. See also http://askubuntu.com/questions/117189/apt-get-install-openjdk-7-jdk-doesnt-install-javac-why .. It works if I write `/usr/lib/jvm/java-7-openjdk-amd64/bin/javac Test.java` instead of `javac Test.java` .. – Håkon Hægland Nov 17 '13 at 09:53
  • 1
    From the second answer (which should have been the accepted one): The proper Debian/Ubuntu way to configure which javac is pointed to by /usr/bin/javac is to use the update-alternatives command. – ljgw Nov 17 '13 at 09:56
  • I have java 8 installed in window's and when I compiled test.java gave error `test.java:10: error: cannot find symbol private static void getAllFilse(File curDir) { ^ symbol: class File location: class test` – Esterlinkof Dec 27 '14 at 11:44
7

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.

Ajeesh
  • 5,650
  • 8
  • 30
  • 52
4

I would check

javac -version

as you might not have Java 7 JDK installed correctly and so only java has been, possibly one the JRE.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Make sure that you've passed the correct address for JAVA_HOME and PATH which is Java 7 installation directories.

Ali Hashemi
  • 3,158
  • 3
  • 34
  • 48
0

I had the same issue. I use Eclipse. There is an option to set JDK Compliance. I set from 1.5 to 1.8 and recompiled the projects. It is fine now. I suggest to set globally and disable this option on project level. enter image description here

David
  • 121
  • 1
  • 3
0

Check if your have the "sourceCompatility" inside your builde.gradle file, example:

sourceCompatibility = 'your_jdk_version'

And check if your: File | Settings | Build, Execution, Deployment | Build Tools | Gradle

Has the Gradle JVM to the selected version.