Javaメモ


Javaメモ

プロパティファイルからデータの取得

import java.util.*;
import java.io.*;

public class HelloWorldGetProperties {
  public static void main(String[] args) {
    try {
      Properties prop = new Properties();
      // プロパティファイルからキーと値のリストを読み込みます
      prop.load(new FileInputStream("javahello.properties"));
      // "javahello.message"に設定されている値を取得します
      String message = prop.getProperty("javahello.message");
      // "javahello.url"に設定されている値を取得します
      String url = prop.getProperty("javahello.url");

      // 読み込んだ値を表示します
      System.out.println("メッセージ:" + message);
      System.out.println("URL:" + url);
      
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

VerifyError?が出た!

コンパイル系のエラー?
プリミティブ型の変数の初期値が設定されていない場合に発生した。

boolean b;  <<< error
boolean b = false; <<< ok

外部コマンドを実行したい

 try {
     // netstatコマンドを実行
     Runtime runtime = Runtime.getRuntime();
     Process process = runtime.exec("netstat");
     // 結果を標準出力に書き出す
     Reader in = new InputStreamReader(process.getInputStream());
     int c = -1;
     while ((c = in.read()) != -1) {
         System.out.print((char) c);
     }
     in.close();
 } catch (Exception ex) {
     ex.printStackTrace();
 }

引数のあるコマンドを実行する場合

String[] cmd = {"move", "a", "b"}
Process process = runtime.exec(cmd);

Windows で実行する場合で、error=2 が返ってくる場合

String[] cmd = {"cmd.exe", "/C", "XXXXXX"}
Process process = runtime.exec(cmd);

JNI

Linuxの場合は、出来上がったlib*.soを、ファイル/etc/ld.so.confか、環境変数LD_LIBRARY_PATHに指定されているディレクトリに置く。

java.lang.UnsatisfiedLinkError? が出る場合は以下を注意

loadLibrary でエラーの場合、LD_LIBRARY_PATH 
関数呼び出しでエラーの場合、.so を生成するときのパッケージ名

OSの名前を取得

   Properties props = System.getProperties();
   System.out.println(props.get("os.name"));

変数の境界値

long64bit9223372036854775807-9223372036854775808
int32bit2147483647-2147483648
double64bit1.7976931348623157E3084.9E-324
float32bit3.4028235E381.4E-45

デフォルト(rt.jar)で組み込まれているクラスより前にパスを通したい

JDK1.4以降ではXMLパーサが組み込まれている。 自分のパーサを通したい場合は以下のように引数を追加する

>java -Xbootclasspath/p:moke.jar;hoge.jar -classpath aa.jar .....

Manifestの情報を取得したい

// 一緒にJARに格納している適当なファイル
URL classURL = getClass().getClassLoader().getResource("file/data1.txt");
URLConnection conn = classURL.openConnection();
JarURLConnection jarConn = (JarURLConnection) conn;
Manifest manifest = jarConn.getManifest();
Attributes attr = manifest.getMainAttributes();
String servletURL = attr.getValue("MOKEMOKE");

Menu

Linux

サーバ構築

Windows

Java

Program

 

最新の20件

2008-04-02 2008-03-11 2008-01-28 2008-01-21 2007-11-27 2007-09-19 2007-06-07
  • Javaメモ
2007-04-11 2007-04-06 2007-03-22 2006-12-20 2006-12-05 2006-12-04 2006-10-27 2006-09-22 2006-07-03 2006-05-16