Showing posts with label java 7. Show all posts
Showing posts with label java 7. Show all posts

10 February 2014

Start Java DB: access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

Since Java 7u51 default network permissions have been restricted, hence Java can not connect to local network sockets by default.
To solve add to $JAVA_HOME/jre/lib/security/java.policy

grant codeBase "file:${java.home}}/../db/lib/*" {
   permission java
.net.SocketPermission "localhost:1527", "listen,resolve";
};
If Java DB (Apache Derby) is installed at another localtion, change the file:/... path accordingly.

Adding the permission line to the global permission section would allow access for all java applications
grant {
 // EXISTING line        // allows anyone to listen on un-privileged ports
 permission java.net.SocketPermission "localhost:0", "listen";
        // new line added
        permission java.net.SocketPermission "localhost:1527", "listen,resolve";
};
 You can also specify port ranges. To allow access to all anonymous ports use
grant {
    permission java.net.SocketPermission "localhost:1024-", "listen,resolve";
};
java.net.SocketPermission JavaDoc

6 November 2013

Java 7 NIO.2 file handling (updated)

Java 7 contains a newer set of IO API's. This is an extension of the NIO (New Input/Output) API introduced in Java 1.4. The extenson is is called NIO.2 and specified in JSR 203. NIO.2 contains
  • a new API for handling filesystems and files
  • an API for asynchronous  I/O on both sockets and files
  • additional features on sockets ( binding, multicast datagrams...)

java.nio.file

In this article we will discuss the file handling API. All the classes for this functionality are in the new java.nio.file package.

The Path class

This is the new class  representing a file location (replacing corresponding methodsd in java.io.File).
The Paths (plural!) class contains static utility methods that return a Path object:
import java.nio.file.Path;
import java.nio.file.Paths; 


// p1: relative Path to a file
Path p1 = Paths.get(“subdir/in.txt"); 
// p2: Absolute path to c:\java\local\jan\Hello.java
// Paths.get() can take a variable number of arguments
// each extra argument indicates an extra directly level
Path p2 = Paths.get("c:\\java\\local”,”rijkswatch”,”Hello.java"); 
In the above example, remark that when using backslashes as a directory separator (Microsoft style), they need to be doubled to escape their special meaning in java String.

FileSystem support

These methods will give you information on the different filesystems on the machine, their mount points, type, size, free space...

The picture shows the output of the code in DOS.

// once more the plural class (FileSystems) contains static utility methods
// that produce objects of the singular (FileSystem) class
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths; 

FileSystem fs = FileSystems.getDefault(); 
for (Path rootPath : fs.getRootDirectories()) { 
  try { 
    FileStore store = Files.getFileStore(rootPath);
    System.out.println(rootPath + ": " + store.type()); 
  } catch (IOException e) { 
    System.out.println(rootPath + ": " + 
      "<error getting store details>"); 
  } 
} 


java.nio.file.Files methods

 

Static utilities in the Files class operate on Path (directory of file) objects.

searching in a directory

for (Path file : Files.newDirectoryStream(Path base, String pattern)){
   …
}
The pattern can contain wildcards like:

* any string
** any string, going down in directories
? any single character
[a,0-9] a or a number
[!a-z] NOT a lower case character
{one,two} String one or two

To recurse into subdirectories use Files.walkFileTree.

Some more static Files methods

public static long size(Path path) // Returns size of the file
public static boolean isReadable(Path path)
public static boolean isSymbolicLink(Path path) // path a soft link? 
  // LinkOption below specifies how symbolic links should be handled 
public static Path createLink(Path link,Path existing) // create hard link
public static Object getAttribute(Path path,String attribute,LinkOption... options)  
  // attribute "unix:nlink" gives # hard links in unix attribute space<
public static boolean exists(Path path, LinkOption... options)
public static boolean isDirectory(Path path, LinkOption... options)
public static Path move(Path source, Path target, CopyOption... options)
public static List<String> readAllLines(Path path, Charset cs) 

Switching between the new nio Path and the old io File

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

Path p1 = Paths.get(“subdir/in.txt");
File f1 = p1.getFile();
Path p2 = f2.getPath();

16 May 2012

Java 7 (Dolphin) new features (edit)

Now that Java 7 will soon become the default Java JRE version for download, let's have a look at some new features:

String switch

You can use a String as a selector for a switch/case statement now

Enhanced try/catch

  • multicatch: catch multiple exceptions in one catch statement
    try {  
     ... 
    
    }catch(IOException | FileNotFoundException ex){…}
  • resources implementing java.lang.AutoCloseable resources (most resources in the JDK) can be automatically closed in a try-with-resources. You do not need a finally clause for this anymore.
    • Java 6 code
      try {                
        Scanner input = new Scanner (new File(“client.txt”));            
        …             
      } finally {             
        input.close();
      }
    • Java 7 code
      try(Scanner input = 
        new Scanner (new File(“client.txt”))){ 
        …
      }
    • You can also have multiple resources in a try
      try(Scanner input = 
        new Scanner (new File(“client.txt”)); 
        Formatter output = 
        new Formatter (new File(“adress.txt”)){ 
        …
      }

NIO.2 revamped file handling

is discussed in a separate blog entry.

Small syntax enhancements

  • Diamond operator
  • When the compiler can infer a generics type in a constructor, you can leave it empty
    List<Currency> currencies= new ArrayList<> ();
  • Literal enhancements
  • // binary numbers
    int localhost=0b1111111000000000000000000000001; 
    //underscores (for readability)
    double million=2_000_000.00; 

static java.util.Objects utilities

Examples:
  • null tolerant equals/hashCode/toString

  • hash (Object... values)

CSS styled javadoc 

With Java 7 Oracle is starting to modernise javadoc. In this release javadoc is restyled:












I'm not a big fan of the new look because the method names do not stand out enough for easy scanning the text for the method you need. Fortunatly the pages are styled using CSS. So I went to the topdirectory of the javadoc API and appended the contents of this file to stylesheet css, et voila:











The -stylesheetfile <path> option can be used to specify a stylesheet other than the default when using the javadoc command to generate the documentation.

Fork/Join

Fork/Join is an additional concurrent ExecutorService. It allows a pool of threads to concurrently work on a task, that can dynamically be split up into parallel subtasks. A busy thread is able to chew of a part of the task he’s doing and make it available for other threads.

JDK7

This is not really part of the Java language, but some tools in the JDK were upgraded.

JavaDB

JavaDB was updated to a more recent version of Apache Derby (10.8). Some noteworthy new features:
  • SQL Sequences
  • In memory databases (with disk based backup/restore)
  • SSL/TLS client/server communication
  • role based authorisations
  • stored procedures with elevated permissions

18 March 2012

NetBeans 7 new features (edit)

NetBeans 7 is approved for release now. Here's a list of new features
  • Remote glassfish interaction
  • Java 7 support
    • Maven 3 support
    • HTML 5 support
    • JSON formatter
    • Git natively integrated
    • JUnit is now an unbundled plugin (Oracle legal had problems with the old CPL license)
    • easy JPA 2 metamodel generation
    The netbeans 7/Java 7/Glassfish 3.1 combination is still a bit flaky though.