3

jlabel付きのjframeを表示する単純なファイルでも、netbeansとeclipseの両方でこれに問題がありました。私のnetbeansのプロジェクトプロパティは、testing2.hihiをメインクラスとして明確に設定しており、distフォルダーに.jarファイルを生成するクリーンおよびビルドを行いました。それをダブルクリックすると、「メインクラスが見つかりませんでした。プログラムは終了します」というメッセージが表示されます。ただし、コマンドプロンプト「java -jar hello2.jar」から実行することを選択すると、通常どおり実行されます!

これは、.jar ファイル内のマニフェスト ファイルです。

      Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_04-b20 (Oracle Corporation)
Class-Path: 
X-COMMENT: src/hihi
Main-Class: testing2.hihi



package testing2;

public class hihi extends javax.swing.JFrame {

/**
 * Creates new form hihi
 */
public hihi() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("hihi");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(50, 50, 50)
            .addComponent(jLabel1)
            .addContainerGap(334, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(33, 33, 33)
            .addComponent(jLabel1)
            .addContainerGap(253, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new hihi().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
4

3 に答える 3

3

Jarファイルに登録されているopenコマンドが正しく設定されていないようです。コマンドラインから確認する方法は次のとおりです(少なくともWindows7では。WindowsVistaでも動作すると確信しています)。

  1. 次のコマンドを入力します。assoc .jar
  2. 印刷する必要があります.jar=jarfile。見つからない場合(症状を考えると、ほとんどあり得ない)、コマンドを使用してエントリを作成しますassoc .jar=jarfile
  3. 次のコマンドを入力します。ftype jarfile
  4. 次のように出力されます
    "C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
    (へのパスはjavaw.exeマシンによって異なる場合があります)。
  5. 定義されていないか、間違った値を出力する場合は、次の方法で修正してください。
    ftype jarfile="C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*

.jarファイルをダブルクリックしても機能するかどうかを確認するには、再起動するか、少なくとも新しいWindowsエクスプローラウィンドウを開く必要がある場合があります。

于 2012-06-04T04:49:05.677 に答える
0

環境変数、JAVA_HOME、CLASS_PATH、および PATH 設定を確認してください。これを確認するには、cmd ウィンドウで %JAVA_HOME% をエコーし​​ます。設定が正しいことを確認してください。

于 2012-06-04T03:28:10.110 に答える
0

Ted Hoppの答え​​は正しいですが、場合によっては何かを変更します。システム環境変数「path」にJavaのパスをすでに設定している場合は、これをコマンドプロンプトに配置できます。

  1. assoc .jar=jarfile

  2. ftype jarfile=javaw.exe -jar %1 %*

JRE パス全体を入力すると、JRE を更新するたびに再度実行する必要があります。この場合、環境変数を変更するだけで、残りはシステムが行います。

于 2012-06-10T22:04:53.377 に答える