3

I have been having problems with this on both netbeans and eclipse even with a simple file that displays a jframe with a jlabel on it. My netbeans's project properties clearly sets testing2.hihi as my Main class and I have clean and build it which produces a .jar file in my dist folder. When I double click on it, it gives me the message" could not find the main class. Program will exit." However, if I choose to run it from the command prompt "java -jar hello2.jar" it will run as normal!

This is the manifest file inside the .jar file.

      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
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Laughy
  • 1,949
  • 5
  • 20
  • 22
  • 2
    What does your manifest file look like? Do you specify a main class? – Hovercraft Full Of Eels Jun 04 '12 at 02:55
  • Sounds like the shell rule for executing .jar files may be borked. What OS are you running? – Ted Hopp Jun 04 '12 at 03:00
  • @Ted Hopp I am running windows vista.May I know which manifest file are you referring to? the one inside the .jar or the one inside the project main folder( sorry,I am still a beginner at this). Anyways I am using netbeans and I have specified the main class to be testing2.hihi under the run tab of project properties. Thanks for all your help! – Laughy Jun 04 '12 at 03:03
  • Ultimately, it is the manifest in the JAR that matters ... if you are executing the JAR. Have you looked at it to check that it is correct? – Stephen C Jun 04 '12 at 03:18
  • @StephenC hi i have added in the manifest files into my code block above. It seems mighty alright to me. Is there anything wrong with it? – Laughy Jun 04 '12 at 03:38
  • Is testing2.hihi a .class file? or was it a .java file? I'm pretty sure you cant put whatever ending you want on there, i think it has to be .class in order for the jar file to know that is a class file – John Jun 04 '12 at 06:02
  • @Laughy - assuming that there is a class file named "testing/hihi.class" in the JAR, that manifest is correct, and the problem is elsewhere ... – Stephen C Jun 04 '12 at 11:04
  • Ultimately it is the manifest *and the contents of the JAR file* that matter. The important thing is that /testing2/hihi.class exists in the JAR file, *under that name,* and has a method `public static void main(String[]).` – user207421 Jun 11 '12 at 12:04

3 Answers3

3

I suspect that the open command registered for Jar files is not set up correctly. Here's a way to check from the command line (at least on Windows 7; I'm pretty sure it works on Windows Vista):

  1. Enter the command: assoc .jar
  2. It should print .jar=jarfile. If it is not found (highly unlikely, given your symptoms), create the entry with the command assoc .jar=jarfile.
  3. Enter the command: ftype jarfile
  4. It should print something like
    "C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
    (The path to javaw.exe might be different on your machine.)
  5. If it isn't defined or prints the wrong value, fix it with:
    ftype jarfile="C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*

You may have to reboot, or at least open a new Windows Explorer window, to see if double-clicking on the .jar file now works.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    Hi thanks alot! This works fine except that my jre is jre7. Evidently, my problem occurs because my jdk is 1.7 and my jre is set to 1.6. Is it possible to set my application to run on older versions of jre? – Laughy Jun 04 '12 at 06:13
  • @Laughy - In Eclipse, you can set the compliance level to 6 under project properties -> Java Compiler. In NetScape, under project properties -> Sources, set the Source/Binary Format to JDK 6. – Ted Hopp Jun 04 '12 at 06:51
0

Please check you environment variables, your JAVA_HOME, CLASS_PATH and PATH setting. you could echo %JAVA_HOME%, in cmd window to check this. make sure you setting is correct.

  • Do I type in echo %JAVA_HOME% into cmd? How do I know whether my settings are correct? Thanks! – Laughy Jun 04 '12 at 03:40
0

The Ted Hopp's answer it's correct but I would change somethings in different cases. If you already set Java's path in the system environment variable "path", you can put this on the command prompt:

  1. assoc .jar=jarfile

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

'cause if you put the entire JRE path, you'll have to do it again every time you update JRE. In this case, you only need to change the environment variable and the system will do the rest.

IvanGrasp
  • 118
  • 10