Thursday, March 13, 2014

Constructor() has private access in Class

If it is obvious for you that this has nothing to do with an issue on granting access, check for version incompatibilities of the .class or the related class.

package java.nio.file does not exist in Mac OSX

This is new addition in java 1.7, so if by default JDK is set as older version, this exception will be given. However, when I check java -version and it says java version "1.7.0_45".

If you have java version specific code in your maven application add the following section in your pom.xml
           

    

   
   










Still it will give the following error:
[ERROR] Failed to execute goal X.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project X: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] javac: invalid target release: 1.7
[ERROR] Usage: javac
[ERROR] use -help for a list of possible options


To solve this issue, set the JAVA_HOME variable to the following using any of the following methods:

// Set JAVA_HOME for one session
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

OR

// Set JAVA_HOME for permanently
vim ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
source .bash_profile
echo $JAVA_HOME

Now compile the application

For those who are curious...

When deciding which JVM to consider for compiling, path specified in JAVA_HOME is used. Here's how to check that.
echo $JAVA_HOME

If it is not specified in JAVA_HOME, using  the following command, you can see where JDK is located in your machine:
which java

It will give something like this: /usr/bin/java

Try this to find where this command is heading to.
ls -l /usr/bin/java

This is a symbolic link to the path /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands

Now try the following command:
cd /System/Library/Frameworks/JavaVM.framework/Versions
ls

Check where "CurrentJDK" version is linked to. (Right click > Get info)
Mine it was  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents.

Version specified as the "currentJDK" will determine which JVM should be used from the available JVMs.

So, this is why I got the "package java.nio.file does not exist" at the first place, as the default referenced JDK is older than 1.7.

How to point Current JDK to correct version?

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/ CurrentJDK

Additional info...

Also, use the following command to verify from where the Java -version is read. (for fun!.. :))
sudo dtrace -n 'syscall::posix_spawn:entry { trace(copyinstr(arg1)); }' -c "/usr/bin/java -version"

It will output something like this:
dtrace: description 'syscall::posix_spawn:entry ' matched 1 probe
dtrace: pid 7584 has exited
CPU     ID                    FUNCTION:NAME
  2    629                posix_spawn:entry   /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java