親ローダーに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"));
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); } }
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/とかに置いてもよいかも)
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) { } }
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);
C:\j2sdk1.4.0\jre\lib\endorsed というディレクトリを作成し、その中に次のjarファイルを入れる。
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
transformer.transform( new DOMSource(document), // new StreamResult(System.out)); new StreamResult(new FileOutputStream(new File("hogehoge.xml"))));
URL property = Object.class.getResource("/config/search.properties");
//プロキシ認証の記述方法 BASE64Encoder encoder = new BASE64Encoder(); urlConnection.setRequestProperty( "Proxy-Authorization", "Basic " + encoder.encode((userid + ":" + password).getBytes()));
String url = "file:" + System.getProperty("user.dir") + System.getProperty("file.separator") + urlstr; newUrl = new URL(url);
>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()); } }
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) { } %>
@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; } }
try { Runtime.getRuntime().exec( new String[] { "rundll32.exe", "url.dll,FileProtocolHandler", "http://jp.sun.com/" }); } catch (IOException e) { e.printStackTrace(); }
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