スキップしてメイン コンテンツに移動

投稿

ラベル(Java 8)が付いた投稿を表示しています

Java 8の機能利用に伴うSpring 4へのバージョンアップ

下記の順序で環境を更新していったときに、問題が起こったのでメモしておきます。 Spring 3 + Java 7: もともとの環境。 Spring 3 + Java 8 (Java 8のラムダ機能などのJava 8からの新機能は未使用): 問題なく動作。 Spring 3 + Java 8 (Java 8のラムダ機能などのJava 8からの新機能を使用): 起動時に下記のExceptionが発生 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL ...(途中省略)... java.lang.ArrayIndexOutOfBoundsException: xxx Googleで調べたところJava 8から導入された新機能を使う場合は、Spring 4に更新しないといけないようです。 Mavenとかは、使っていなかったので、Spring Frameworkから提供されているspring-xxx系のjarすべてをバージョン3.X系からバージョン4.X系のものに手動で置き換えました。 (Spring Frameworkはバージョン5.X系ももう出ているのですね。既に置いていかれているorz...) 無事にアプリケーションが起動するようになりました! ただ普通は、こんな乱暴にjarを入れ替えることは難しいので、アップデートはもっと慎重にやるべきでしょうね。

Google App Engineで外部のURLにアクセス

Google App Engine(GAE)から外部のネットワークへ接続する際の注意 Google App Engine(GAE)の開発環境の移行の際にJava 7からJava 8へ移行しました。 その際、GAEから、java.net.URL.openConnectionを使って普通に外部ネットワークのAPIを呼び出そうとすると下記のエラーが発生するようになってしまいました。 java.net.UnknownHostException: ホスト名 いろいろ調べていくと、下記のページにたどり着きました。 https://cloud.google.com/appengine/docs/standard/java/issue-requests 上記の表を間単に日本語訳すると URLのフェッチ方法 Java 7 Java 8 UrlFetch API Calls com.google.appengine.urlfetch.*以下のクラスを使う。無料で使用できる部分あり。 com.google.appengine.urlfetch.*以下のクラスを使う。無料で使用できる部分あり。 Javaネイティブのjava.net.URL.openConnectionなどを使う方法 無料ユーザも制限なく利用可能。 無料ユーザは、利用不可。 java.net.UnknownHostException、java.net.SocketTimeoutException、java.io.IOException とかが投げられる。 まとめると、 GAEで外部ネットワークへアクセスする方法には「GAEが提供するUrlFetch API Calls」「Javaネイティブのjava.net.URL.openConnection」の2つ方法がある。 Java 7では、「GAEが提供するUrlFetch API Calls」「Javaネイティブのjava.net.URL.openConnection」は無料でどちらの方法も利用可能。 Java 8で無料で利用できるのは「GAEが提供するUrlFetch API Calls」のみ。 Java 8で「Javaネイティブのjava.net.URL.openConnectionの方法」は課金ユーザーのみ利

Goolge App Engineの開発環境の更新 (Java)

Google Plugins for EclipseからCloud Tools for Eclipseの移行 長らくEclipse Luna (4.4) + Google Plugin for Eclipseで開発を続けてきたのですが、Google App Engineの開発ページを覗いたら下記のようなメッセージが出ていてびっくり。 The Google Plugin for Eclipse is deprecated and will be removed in January 2018. Migrate to Cloud Tools for Eclipse and/or the GWT Eclipse Plugin as soon as possible to avoid disruption. Google Plugin for Eclipseは、2018年の1月でサポート打ち切りで、Cloud Tools for Eclipseに移行しなさいとのこと。 せっかくなので最新のeclipseを使って下記の設定で開発環境を再構築することにしました。 移行方法 最新のJDK 8 を http://www.oracle.com/technetwork/java/javase/downloads/index.html からダウンロードしてインストール。JDKをダウンロード Java 9 も試そうかとも思ったのですが、2017年9月現在Google App Engine側はまだJava 8までしかサポートしていないので、 Eclipseを  http://www.eclipse.org/downloads/eclipse-packages/  からダウンロード。私は、Eclipse IDE for Java Developersをダウンロードしました。 EclipseでGoogle App Engineの開発を行うための設定を https://cloud.google.com/eclipse/docs/quickstart  を読んで実施。 Google Cloud SDKをダウンロード Google Cloud Tools for Eclipse をEclipse Marketplace.を通してインストール 古いプロジェクト

Java: Identify Country From IP Address

Identify Country From IP Address Many people sometimes would like to identify country from IP address when you check access log or something. Most of the people google with keywords like "ip address country" or "whois ip" or something, and then use the internet service which they find. In this post, I will show you program for identifying country from IP address. I wrote the program in Java, but if you an average developer you can easily translate into the program language you prefer. Using File Provided by RIR IP address is allocated, registered and managed by regional internet registry ( RIR ). There are five organization based on covering region: African Network Information Centre (AfriNIC): Africa American Registry for Internet Numbers (ARIN): the United States, Canada, several parts of the Caribbean region, and Antarctica. Asia-Pacific Network Information Centre (APNIC): Asia, Australia, New Zealand, and neighboring countries Latin America and

Java: Get Sorted Array Index

As you know, if you would like to sort array, you should use Arrays.sort method. But if you would like to get sorted index, you should use your a brain a bit. In this post, I will show you the code snippet for getting sorted array index. The following is the simplest way to get sorted index. The disadvantages of the following code are autoboxing in compare process and returned array type is not int[] but Integer[]. /** * The most common & general way. But this method use autoboxing and need Integer[] not int[]. */ public static final <T>void sort(T[] array, Comparator<T> comparator, Integer[] sorted) { Arrays.sort(sorted, (i1, i2) -> comparator.compare(array[i1], array[i2])); } To solve these advantage, I wrote following array sort helper class which can get sorted array index as int[] type. You can call getSortedIndex method with source array and comparator as the arguments. The code uses lambda expression which was introduced in Java 8. packa

Java: Get All Static Fields Defined in Class by Reflection

If you want to get all static fields in a class, use code snippet below. The key part is "Modifier.isStatic", "getDeclaredFields" methods package com.dukesoftware.reflection; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ReflectionUtils { public static List<Field> getStaticFields(Class<?> clazz) { Field[] declaredFields = clazz.getDeclaredFields(); return Arrays.stream(declaredFields) .filter(field -> Modifier.isStatic(field.getModifiers())) .collect(Collectors.toList()) ; } }

Java 8: Reduce Redundant Code Using Lambda Expression

Redundant Code Problem If you are working on mathematical program, you might have written code for creating new Function object from source function object. For example, creating new function "g(x)" which is defined as "f(x) - target" Before Java 8, this kind of creating new function object needs redundant code as below: // using anonymous inner class public final static Function subtract(Function f, double target) { return new Function() { @Override public double f(double x) { return f.f(x) - target; } }; } // function interface definition public interface Function { double f(double x); } Of course, the code is perfect but the redundant part "new Function()..." prevent developers understanding core logic part quickly. Lambda Expression Now in Java 8, "lambda expression" helps reducing this kind of redundant code. See below code which uses lambda expression. public final stat