Xerces関連の障害。

親ローダーにxml-apis.jar、子ローダーにxercesImpl.jarをロードさせたところ、

loader constraints violated when linking org/w3c/dom/Document class 

とエラーになった。xml-apis.jarも子ローダに読み込ませることで、解決。

解決。

System.out.println("Loaded: "+ clazz.getResource("/"+clazz.getName().replace('.','/')+".class"));

ロードされているクラスが、どのファイルからロードされているかを確認する*1

public void print(String clazz) {
	ClassLoader loader = getClass().getClassLoader();
	System.out.println("----- " + clazz + " -----");
	try {
		Enumeration enum = loader.getResources(clazz);
		while (enum.hasMoreElements()) {
			URL resource = (URL) enum.nextElement();
			System.out.println(resource);
		}
	} catch (IOException ex) {
		ex.printStackTrace(System.err);
	}
}

Class#getResource() をいろいろな環境で

Tomcatでは

System.out.println(Log4jInitServlet.class.getResource("/"));
System.out.println(Log4jInitServlet.class.getResource("."));
System.out.println(Log4jInitServlet.class.getResource("lib"));
System.out.println(Log4jInitServlet.class.getResource("/../lib/"));

の結果は

file:/C:/Documents and Settings/xxxx/My Documents/IBM/wssd/workspace/xxxx/Web Content/WEB-INF/classes/
null
null
file:/C:/Documents and Settings/xxxx/My Documents/IBM/wssd/workspace/xxxx/Web Content/WEB-INF/lib/

WASでは(正確にはWSSD)

file:/C:/Program%20Files/IBM/WebSphere%20Studio/Site%20Developer/v5.1/runtimes/base_v5/properties/
file:/C:/Documents and Settings/m-kino/My Documents/IBM/wssd/workspace/FrameworkWebApplication/Web Content/WEB-INF/classes/kino/servlet/init/
null
file:/C:/Program Files/IBM/WebSphere Studio/Site Developer/v5.1/runtimes/base_v5/java/lib/

おいっっ

ポータブルにパスを取得する?(まだ)

System.out.println(JavaDocumentSearcher.class.getResource("/")); <-指定したクラスの、ロードされたパス
System.out.println(JavaDocumentSearcher.class.getResource("/../lib/")); <-指定したクラスの、ロードされたパスからの相対パス(決定されたパスがクラスパスが通ってたら。)

結局

URL url = getClass().getResource("/kino/log4j/log4j.xml");

などして、classesにkino/log4j/log4j.xmlを配置した。(クライアントアプリなど、自らクラスパスを通せる環境だったら、lib/とかに置いてもよいかも)

JSPやServletのOutにDOMを書き出したい。

public void write(HttpServletRequest request, HttpServletResponse response) {
	response.setContentType("text/xml; charset=UTF-8");
	try {
		PrintWriter out = response.getWriter();
		transformer.transform(new DOMSource(getDocument()),
		new StreamResult(out));
	} catch (IOException e) {
	} catch (TransformerException e) {
	}
}

JDKで使用している、XML実装のVersionの調べ方。

System.out.println(org.apache.xerces.impl.Version.fVersion);
org.apache.xalan.Version.main(args);

try {
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	DocumentBuilder builder = factory.newDocumentBuilder();
	Document document = builder.parse(xml);
	//			print(document);

	TransformerFactory tfactory = TransformerFactory.newInstance();
	Transformer transformer = tfactory.newTransformer(new StreamSource(xsl));

	System.out.println(builder);
	System.out.println(transformer);

JDKのJAXPの実装の入れ替え方法

C:\j2sdk1.4.0\jre\lib\endorsed というディレクトリを作成し、その中に次のjarファイルを入れる。

xslでスペース( )を入れたい

<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

XSLT変換

transformer.transform(
 new DOMSource(document),
//	new StreamResult(System.out));
new StreamResult(new FileOutputStream(new File("hogehoge.xml"))));

ファイルのURLを取得する

URL property = Object.class.getResource("/config/search.properties");

プロキシ越えとか、HttpConnection?

fileplain
//プロキシ認証の記述方法
BASE64Encoder encoder = new BASE64Encoder();
urlConnection.setRequestProperty(
 "Proxy-Authorization",
 "Basic " + encoder.encode((userid + ":" + password).getBytes()));

URL

String url = "file:" + System.getProperty("user.dir") + System.getProperty("file.separator") + urlstr;
newUrl = new URL(url);

javaの実行

>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt.jar" kino.swt.JavaDocSearchForm
>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt.jar;lib\kino_javadocsearch.jar" 
 kino.swt.JavaDocSearchForm
>java -classpath "クラスパス(セミコロン区切り)" クラス

このサイトよいかも。

カレントディレクトリ取得方法

SUNのお墨付きとのこと

File fileCurrent=new File(".");
System.out.println(fileCurrent.getAbsolutePath());

ディレクトリ文字列のみ取得する方法にアレンジ

import java.io.*;

public class MyTest{
	public static void main(String[] args){
		File fileParentDirectory=new File(".").getAbsoluteFile().getParentFile();
		System.out.println(fileParentDirectory.getAbsolutePath());
	}
}

WEBアプリケーションのルートを取得してパスを生成する

String root_path = this.getServletContext().getRealPath("/");
String file_path = root_path + "hoge.dat"

システムのプロパティを見る方法

<%
	try {
		Properties props = System.getProperties();
		Enumeration keys = props.propertyNames();
		for (int i = 1; keys.hasMoreElements(); i++) {
			String key = (String)keys.nextElement();
			String val = props.getProperty(key);
%>
	<tr>
		<td nowrap><font size=-1><%=i%></font></td>
		<td nowrap><font size=-1><%=key%></font></td>
		<td nowrap><font size=-1><%=val%></font></td>
	</tr>
<%
	}
	} catch (Exception ex) {
	}
%>

Javadoc書き方

@see	UserInformationManager#getUserInformation(String)

とすると、リンクが張られる。

{@link	InformationController	InformationController}のファイル読み込みの実装です。

なんて使い方もできる

コレクションを配列に変換

type: 型
collection: コレクションクラス
(type[]) collection.toArray(new type[collection.size()]);

配列をコレクションに変換

fileList: Object[]型
java.util.Arrays.asList(fileList))

ファイルを読み込む

BufferedReader reader = null;
try {
	reader = new BufferedReader(new FileReader(path));
	//もしくは
	//new BufferedReader(
	//  new InputStreamReader(url.openStream(), "JISAutoDetect"));
  
	String line;
	while ((line = reader.readLine()) != null) {
		buffer.append(line);
		buffer.append("\n");
	}
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	if (reader != null) {
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		reader = null;
	}
}

Javaから、IEを起動(Windowsのみ)

try {
	Runtime.getRuntime().exec(
		new String[] {
			"rundll32.exe",
			"url.dll,FileProtocolHandler",
			"http://jp.sun.com/" });
} catch (IOException e) {
	e.printStackTrace();
}

Objectのシリアライズ

try {
	//FileInputStreamオブジェクトの生成
	FileInputStream inFile =
		new FileInputStream([serializeしたファイル名]);

	//ObjectInputStreamオブジェクトの生成 
	ObjectInputStream inObject = new ObjectInputStream(inFile);

	Object object = (クラス名) inObject.readObject();

	inObject.close(); //オブジェクト入力ストリームのクローズ
	inFile.close(); //ファイル入力ストリームのクローズ

	//FileOutputStreamオブジェクトの生成
	FileOutputStream outFile =
		new FileOutputStream([書き出したいファイル名]);

	//ObjectOutputStreamオブジェクトの生成 
	ObjectOutputStream outObject = new ObjectOutputStream(outFile);

	//クラスVectorのオブジェクトの書き込み
	outObject.writeObject(object);

	outObject.close(); //オブジェクト出力ストリームのクローズ
	outFile.close(); //ファイル出力ストリームのクローズ

} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (ClassNotFoundException e) {
}

インナクラス、無名クラス

インナークラス・無名クラスは、フィールドにはアクセス可能、ローカル変数には不可能。


現在のアクセス:23762


*1 でもこれ、候補しかわかりませんね

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS