Eclipse上でJSPでエラーとなる

JSPのエディタ上で

javax.servlet.jsp.PageContext cannot be resolved to a type

とか、JspExceptionとかで赤くなるケースがある。バリデーションの設定を変更するのは面倒だけど、pom.xmlに

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>

という感じで追加しておけば改善される。

Ubuntuでjavaのalternativesを変更する

UbuntuでJava 8をデフォルトで使おうと思うと、alternativesの値を変更する必要があると思うけど、update-alternatives -allで確認すると結構あるのでまとめておく。たとえば、/usr/lib/jvm/jdk1.8.0にJava 8を置いたら以下の感じ。

sudo update-alternatives --install "/usr/bin/ControlPanel" "ControlPanel" "/usr/lib/jvm/jdk1.8.0/jre/bin/ControlPanel" 1
sudo update-alternatives --install "/usr/bin/appletviewer" "appletviewer" "/usr/lib/jvm/jdk1.8.0/bin/appletviewer" 1
sudo update-alternatives --install "/usr/bin/extcheck" "extcheck" "/usr/lib/jvm/jdk1.8.0/bin/extcheck" 1
sudo update-alternatives --install "/usr/bin/idlj" "idlj" "/usr/lib/jvm/jdk1.8.0/bin/idlj" 1
sudo update-alternatives --install "/usr/bin/jar" "jar" "/usr/lib/jvm/jdk1.8.0/bin/jar" 1
sudo update-alternatives --install "/usr/bin/jarsigner" "jarsigner" "/usr/lib/jvm/jdk1.8.0/bin/jarsigner" 1
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0/jre/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javadoc" "javadoc" "/usr/lib/jvm/jdk1.8.0/bin/javadoc" 1
sudo update-alternatives --install "/usr/bin/javafxpackager" "javafxpackager" "/usr/lib/jvm/jdk1.8.0/bin/javafxpackager" 1
sudo update-alternatives --install "/usr/bin/javah" "javah" "/usr/lib/jvm/jdk1.8.0/bin/javah" 1
sudo update-alternatives --install "/usr/bin/javap" "javap" "/usr/lib/jvm/jdk1.8.0/bin/javap" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.8.0/jre/bin/javaws" 1
sudo update-alternatives --install "/usr/bin/jcmd" "jcmd" "/usr/lib/jvm/jdk1.8.0/bin/jcmd" 1
sudo update-alternatives --install "/usr/bin/jconsole" "jconsole" "/usr/lib/jvm/jdk1.8.0/bin/jconsole" 1
sudo update-alternatives --install "/usr/bin/jcontrol" "jcontrol" "/usr/lib/jvm/jdk1.8.0/jre/bin/jcontrol" 1
sudo update-alternatives --install "/usr/bin/jdb" "jdb" "/usr/lib/jvm/jdk1.8.0/bin/jdb" 1
sudo update-alternatives --install "/usr/bin/jexec" "jexec" "/usr/lib/jvm/jdk1.8.0/jre/lib/jexec" 1
sudo update-alternatives --install "/usr/bin/jhat" "jhat" "/usr/lib/jvm/jdk1.8.0/bin/jhat" 1
sudo update-alternatives --install "/usr/bin/jinfo" "jinfo" "/usr/lib/jvm/jdk1.8.0/bin/jinfo" 1
sudo update-alternatives --install "/usr/bin/jmap" "jmap" "/usr/lib/jvm/jdk1.8.0/bin/jmap" 1
sudo update-alternatives --install "/usr/bin/jps" "jps" "/usr/lib/jvm/jdk1.8.0/bin/jps" 1
sudo update-alternatives --install "/usr/bin/jrunscript" "jrunscript" "/usr/lib/jvm/jdk1.8.0/bin/jrunscript" 1
sudo update-alternatives --install "/usr/bin/jsadebugd" "jsadebugd" "/usr/lib/jvm/jdk1.8.0/bin/jsadebugd" 1
sudo update-alternatives --install "/usr/bin/jstack" "jstack" "/usr/lib/jvm/jdk1.8.0/bin/jstack" 1
sudo update-alternatives --install "/usr/bin/jstat" "jstat" "/usr/lib/jvm/jdk1.8.0/bin/jstat" 1
sudo update-alternatives --install "/usr/bin/jstatd" "jstatd" "/usr/lib/jvm/jdk1.8.0/bin/jstatd" 1
sudo update-alternatives --install "/usr/bin/jvisualvm" "jvisualvm" "/usr/lib/jvm/jdk1.8.0/bin/jvisualvm" 1
sudo update-alternatives --install "/usr/bin/keytool" "keytool" "/usr/lib/jvm/jdk1.8.0/jre/bin/keytool" 1
sudo update-alternatives --install "/usr/lib/mozilla/plugins/libnpjp2.so" "libnpjp2.so" "/usr/lib/jvm/jdk1.8.0/jre/lib/amd64/libnpjp2.so" 1
sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/usr/lib/jvm/jdk1.8.0/jre/lib/amd64/libnpjp2.so" 1
sudo update-alternatives --install "/usr/bin/native2ascii" "native2ascii" "/usr/lib/jvm/jdk1.8.0/bin/native2ascii" 1
sudo update-alternatives --install "/usr/bin/orbd" "orbd" "/usr/lib/jvm/jdk1.8.0/jre/bin/orbd" 1
sudo update-alternatives --install "/usr/bin/pack200" "pack200" "/usr/lib/jvm/jdk1.8.0/jre/bin/pack200" 1
sudo update-alternatives --install "/usr/bin/policytool" "policytool" "/usr/lib/jvm/jdk1.8.0/jre/bin/policytool" 1
sudo update-alternatives --install "/usr/bin/rmic" "rmic" "/usr/lib/jvm/jdk1.8.0/bin/rmic" 1
sudo update-alternatives --install "/usr/bin/rmid" "rmid" "/usr/lib/jvm/jdk1.8.0/jre/bin/rmid" 1
sudo update-alternatives --install "/usr/bin/rmiregistry" "rmiregistry" "/usr/lib/jvm/jdk1.8.0/jre/bin/rmiregistry" 1
sudo update-alternatives --install "/usr/bin/schemagen" "schemagen" "/usr/lib/jvm/jdk1.8.0/bin/schemagen" 1
sudo update-alternatives --install "/usr/bin/serialver" "serialver" "/usr/lib/jvm/jdk1.8.0/bin/serialver" 1
sudo update-alternatives --install "/usr/bin/servertool" "servertool" "/usr/lib/jvm/jdk1.8.0/jre/bin/servertool" 1
sudo update-alternatives --install "/usr/bin/tnameserv" "tnameserv" "/usr/lib/jvm/jdk1.8.0/jre/bin/tnameserv" 1
sudo update-alternatives --install "/usr/bin/unpack200" "unpack200" "/usr/lib/jvm/jdk1.8.0/jre/bin/unpack200" 1
sudo update-alternatives --install "/usr/bin/wsgen" "wsgen" "/usr/lib/jvm/jdk1.8.0/bin/wsgen" 1
sudo update-alternatives --install "/usr/bin/wsimport" "wsimport" "/usr/lib/jvm/jdk1.8.0/bin/wsimport" 1
sudo update-alternatives --install "/usr/bin/xjc" "xjc" "/usr/lib/jvm/jdk1.8.0/bin/xjc" 1

そして、

sudo update-alternatives --all

で全部の設定が聞かれるので対象箇所を変更しておく。

PDFBoxでPDFに画像を差し込む

PDFBoxを使うと、既存のPDFにjpg画像を差し込むことができる。実装例としてはここにあるけど、以下のような感じ。

PDDocument doc = null;
try
{
    doc = PDDocument.load( inputFile );
    //we will add the image to the first page.
    PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get( 0 );
    PDXObjectImage ximage = null;
    if( image.toLowerCase().endsWith( ".jpg" ) )
    {
        ximage = new PDJpeg(doc, new FileInputStream( image ) );
    }
    else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
    {
        ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
    }
    else
    {
        //BufferedImage awtImage = ImageIO.read( new File( image ) );
        //ximage = new PDPixelMap(doc, awtImage);
        throw new IOException( "Image type not supported:" + image );
    }
    PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
    contentStream.drawImage( ximage, 20, 20 );
    contentStream.close();
    doc.save( outputFile );
}
finally
{
    if( doc != null )
    {
        doc.close();
    }
}

ポイントは

  • contentStreamの前にximageを取得しておくこと
  • drawImageが左下から始まっていること

かな。