razshan
razshan

Reputation: 1138

JAR exits after the first class is complete

I have two classes. I created a JAR file using:

jar cvf practice.jar class1.class class2.class

Then I set the starting entry point:

jar cfe practice.jar class1 class1.class

When I execute the JAR file, it works fine until the point where there is a transition to the next class, i.e. class2 hey = new class2(); Then it exits out. But want to continue to the next class.

It should go to class2.class. Since it is saying in that object. Apparently, it does not.

public class class1 {
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null, "This is class 1", "Order",JOptionPane.PLAIN_MESSAGE);
        class2 hey = new class2();
    }
}

public class class2 {

    public class2() {
        JOptionPane.showMessageDialog(null, "This is class 2", "Order",JOptionPane.PLAIN_MESSAGE);
    }
}

Upvotes: 1

Views: 143

Answers (3)

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24281

I tried your example and I get a NoClassDefFound for class2 after pressing "OK" in the first window. That is because the class2.class is not present in the practice.jar file.

Try the following instead of both your jar ... commands:

jar cfe practice.jar class1 class1.class class2.class

Upvotes: 0

Kennet
Kennet

Reputation: 5796

The only code written in class2 is in the main method, this method is not executed when creating an instance of that class. If you either move the code to the constructor or call the main method:

public class Class1 {

public Class1() {
    JOptionPane.showMessageDialog(null, "This is class 1", "Order",
            JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args) {
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();
}

}

public class Class2 {
public Class2() {
    JOptionPane.showMessageDialog(null, "This is class 2", "Order",JOptionPane.PLAIN_MESSAGE);
}

}

Upvotes: 1

stu
stu

Reputation: 8805

You're making a new instance of the object class2 but if there's nothing in the constructor nothing will happen, my guess is that you have code in another method in class2 that you need to call?

I guess it would also be helpful to know what you mean by "exits out."

Upvotes: 1

Related Questions