0

jarA がクラスパスにある場合は何かを行い、jarB がクラスパスにある場合は別のことをしたいと思います (それが可能かどうかはわかりません)。どの jar が使用されるか分からないため、Netbeans プロジェクト ライブラリ リファレンスでこれらの jar を指定するつもりはありません。

リフレクションを通じてjarのクラスを使用しようとすると、Netbeansプロジェクトライブラリ参照にjarを含めることが機能するようになりました。しかし、netbeans プロジェクト ライブラリの参照を削除しても、クラスパスに jar を追加すると、リフレクションが機能しません。

私の質問は本当に 1) これを行うことができますか? 2) 私はそれについて正しく考えていますか? 3) -cp または -classpath を指定して jar を含むディレクトリを含めると、それが機能しないのはなぜですか? 4) jar ファイルの manifest.mf でディレクトリを指定すると、なぜ機能しないのですか?

私にお知らせください。これは本当に私を悩ませています。

ありがとう、ジュリアン

4

4 に答える 4

1

ポイント3-ディレクトリだけでなく、完全修飾jar名をクラスパスに含める必要があります。

于 2009-06-29T22:28:04.547 に答える
1

クラスパスは、.classファイルを含むディレクトリを参照することも、.jarファイルを直接参照することもできます。.jarファイルを含むディレクトリを参照している場合、それらは含まれません。


java -help-classpath「クラスファイルを検索するためのディレクトリ、JARアーカイブ、およびZIPアーカイブのリスト 」これは、クラスパス上のディレクトリがJARアーカイブではなく、クラスファイルを検索することを非常に明確に示しています。

于 2009-06-29T22:29:40.240 に答える
1

そう信じる!

ClassLoader.getSystemClassLoader()getURLs();

これにより、クラスパスにある Jar ファイルがわかります。次に、X または Y を好きなように実行します。

于 2009-06-29T22:33:26.643 に答える
0

これが私がやっている方法です。内部クラスは、ロガーのシングルトン インスタンスとそのトレース メソッドをカプセル化します (シングルトン内のシングルトン)。外部クラスは、特別なクラスをロードできる場合にのみそれを使用します。それ以外の場合は、それなしで続行します。うまくいけば、これをニーズに合わせて変更できます。そして、より良いコードに関する提案はいつでも大歓迎です! :-) HTH

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Provides centralized access to standardized output formatting. Output is sent to System.out and,
 * if the classpath allows it, to the Cisco CTIOS LogManager.  (This is NOT a dependency, however.)
 * 
 */
public class LogWriter
{
    protected static LogWriter me = null;
    private SimpleDateFormat dateFormat = null;
    private StringBuffer line = null;
    CLogger ciscoLogger = null;

    /*
     * The following 2 methods constitute the thread-safe singleton pattern.
     */
    private static class LogWriterHolder 
    {
        public static LogWriter me = new LogWriter();
    }
    /**
     * Returns singleton instance of the class.  Thread-safe.  The only way to get one is to use this.
     * 
     * @return an instance of LogWriter
     */
    public static LogWriter sharedInstance() 
    {
        return LogWriterHolder.me;
    }


    @SuppressWarnings("unchecked")
    LogWriter() 
    {
        dateFormat = new SimpleDateFormat("yyyyMMddHHmmss ");
        line = new StringBuffer();
        try {
            Class x = Class.forName("com.cisco.cti.ctios.util.LogManager");
            if( x != null ) {
                java.lang.reflect.Method m = x.getMethod("Instance", new Class[0]);
                java.lang.reflect.Method n  = x.getMethod("Trace", int.class, String.class );
                if( m != null ) {
                     Object y = m.invoke( x , new Object[0] );
                     if( n != null ) {
                         ciscoLogger = new CLogger();
                         ciscoLogger.target = y;
                         ciscoLogger.traceImpl = n ;
                     }
                }
            }
        } catch(Throwable e ) 
        {
            System.err.println( e.getMessage() ) ;
            e.printStackTrace();
        }
    }

    /**
     * Formats a line and sends to System.out.  The collection and formatting of the text is 
     * thread safe.
     * 
     * @param message       The human message you want to display in the log (required).
     * @param hostAddress   Host address of server (optional)
     * @param hostPort      Port on hostAddresss (optional) - also used for className in object-specific logs.
     */
    public void log( String message, String hostAddress, String hostPort ) 
    {
        if ( message == null || message.length() < 3 ) return;

        synchronized( this ) 
        {
            try {
                line.delete(0, line.length());
                line.append(dateFormat.format(new Date()));
                line.append(hostAddress);
                line.append(":");
                line.append(hostPort);
                line.append(" ");
                while (line.length() < 28)
                    line.append(" ");
                line.append(message);

                this.write( line.toString() );

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void write(String line )
    {
        System.out.println( line ) ;
    }

    /**
     * Write a simple log message to output delegates (default is System.out).
     * <p>
     * Will prepend each line with date in yyyyMMddHHmmss format.  there will be a big space
     * after the date, in the spot where host and port are normally written, when {@link LogWriter#log(String, String, String) log(String,String,String)}
     * is used.
     * 
     * @param message What you want to record in the log.
     */
    public void log( String message )
    {
        if( ciscoLogger != null ) ciscoLogger.trace(0x01, message );
        this.log( message, "", "");
    }

    class CLogger
    {
        Object target;
        Method traceImpl;

        @SuppressWarnings("boxing")
        public void trace( int x, String y )
        {
            try {
                traceImpl.invoke( target, x, y) ;
            } catch( Throwable e ) {
                // nothing to say about this
            }
        }
    }

}
于 2009-08-12T23:02:55.250 に答える