Test Program on java.activation.DataSource

Q

How to write a simple test program on java.activation.DataSource? I want to try the JAF (JavaBeans Activation Framework) included in JDK.

✍: FYIcenter.com

A

JAF (JavaBeans Activation Framework) API 1.1 has been included in JDK since 1.6. You use the following simple test program, DataSourceTest.java, to try it:

// Copyright (c) 2015 FYIcenter.com
import javax.activation.*;
public class DataSourceTest {
   public static void main(String[] args) 
      throws Exception {
      String filename = args[0];
      DataSource ds = new FileDataSource(filename);
      System.out.println("getName() = "+ds.getName() );
      System.out.println("getContentType() = "
         +ds.getContentType() );
   }
}

Compile and run with only JDK and no activation.jar:

\fyicenter>java -version
java version "1.7.0_45"

\fyicenter>javac -classpath . DataSourceTest.java

\fyicenter>java -classpath . DataSourceTest
getName() = DataSourceTest.java
getContentType() = application/octet-stream

The above session shows that my JAF configuration does not recognize *.java files as text files. Here is how to change the JAF configuration:

Edit .mime.types in my home folder c:\users\fyicenter and add the following line. Then run the test program again:

\fyicenter>edit c:\users\fyicenter\.mime.types 
text/plain		txt text TXT TEXT java

\fyicenter>java -classpath . DataSourceTest
getName() = DataSourceTest.java
getContentType() = text/plain

Note that the .mime.types file defines mapping of a MIME types with file extensions with a tab character.

Back to FAQ for JAF (JavaBeans Activation Framework).

2016-07-09, 1341👍, 0💬