Version
ロード時にマニフェストからバージョン番号を読み取り、Version.MAJOR
他の場所で必要な場所などを参照するアプリのクラスを作成しようとしています。しかし、私はそうすることで問題を抱えています。これが私の現在のコードです:
public class Version {
public static final int APPCODE;
public static final int MAJOR;
public static final int MINOR;
public static final char RELEASE;
public static final int BUILD;
static {
try {
Class clazz = Version.class;
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString();
if (classPath.startsWith("jar")) {
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
Manifest manifest = new Manifest(new URL(manifestPath).openStream());
Attributes attr = manifest.getMainAttributes();
APPCODE = Integer.parseInt(attr.getValue("APPCODE"));
MAJOR = Integer.parseInt(attr.getValue("MAJOR"));
MINOR = Integer.parseInt(attr.getValue("MINOR"));
RELEASE = attr.getValue("RELEASE").charAt(0);
BUILD = Integer.parseInt(attr.getValue("BUILD"));
}
} catch (IOException e) {
System.exit(9001);
}
}
}
変数が初期化されていない可能性があるためstatic final
(たとえば、間違ったマニフェストがロードされているか、例外がロードされている場合)、これを行うための正しい手順がわかりません。
この質問を読んで、使用しないという洞察が得られましたpublic static final
。public static
ゲッターメソッドを使用する必要がありますか?