0

Ok, so, for some reason, my .jar wont execute even though it does in eclipse. Heres my code, its not the best but, im experimenting. I need some help in getting it to execute outside of eclipse as the .jar file.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;  // only need this one class
import javax.swing.*;
////////////////////////////////////////////////////////////////// TextClock
public class CopyOftheclock {
//================================================================= main
public static void main(String[] args) {
    JFrame clock = new TextClockWindow();
    clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    clock.setVisible(true);
}//end main
}//endclass TextClock


@SuppressWarnings("serial")
//////////////////////////////////////////////////////////// TextClockWindow
class TextClockWindow extends JFrame {
//=================================================== instance variables
private JTextField timeField;  // set by timer listener

//========================================================== constructor
public TextClockWindow() {
    // Build the GUI - only one panel
    timeField = new JTextField(7);
    timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

    Container content = this.getContentPane();
    content.setLayout(new FlowLayout());
    content.add(timeField); 

    this.setTitle("Norway");
    this.pack();

    // Create a 1-second timer and action listener for it.
    // Specify package because there are two Timer classes
    javax.swing.Timer t = new javax.swing.Timer(1000,
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String a = "";
                  Calendar now = Calendar.getInstance();
                  int h = now.get(Calendar.HOUR_OF_DAY);
                    if (h==24)
                    {
                        h=8;
                        a = "A.M";
                    }
                    if (h==1)
                    {
                        h=9;
                        a = "A.M";
                    }
                    if (h==2)
                    {
                        h=10;
                        a = "A.M";
                    }
                    if (h==3)
                    {
                        h=11;
                        a = "A.M";
                    }
                    if (h==4)
                    {
                        h=12;
                        a = "P.M";
                    }
                    if (h==5)
                    {
                        h=1;
                        a = "P.M";
                    }
                    if (h==6)
                    {
                        h=2;
                        a = "P.M";
                    }
                    if (h==7)
                    {
                        h=3;
                        a = "P.M";
                    }
                    if (h==8)
                    {
                        h=4;
                        a = "P.M";
                    }
                    if (h==9)
                    {
                        h=5;
                        a = "P.M";
                    }
                    if (h==10)
                    {
                        h=6;
                        a = "P.M";
                    }
                    if (h==11)
                    {
                        h=7;
                        a = "P.M";
                    }
                    if (h==12)
                    {
                        h=8;
                        a = "P.M";
                    }
                    if (h==13)
                    {
                        h=9;
                        a = "P.M";
                    }
                    if (h==14)
                    {
                        h=10;
                        a = "P.M";
                    }
                    if (h==15)
                    {
                        h=11;
                        a = "P.M";
                    }
                    if (h==16)
                    {
                        h=12;
                        a = "P.M";
                    }
                    if (h==17)
                    {
                        h=1;
                        a = "A.M";
                    }
                    if (h==18)
                    {
                        h=2;
                        a = "A.M";
                    }
                    if (h==19)
                    {
                        h=3;
                        a = "A.M";
                    }
                    if (h==20)
                    {
                        h=4;
                        a = "A.M";
                    }
                    if (h==21)
                    {
                        h=5;
                        a = "A.M";
                    }
                    if (h==22)
                    {
                        h=6;
                        a = "A.M";
                    }
                    if (h==23)
                    {
                        h=7;
                        a = "A.M";
                    }
                  int m = now.get(Calendar.MINUTE);
                  int s = now.get(Calendar.SECOND);
                  timeField.setText("" + h + ":" + m + ":" + s + " " + a);
              }

          });
    t.start();  // Start the timer
}//end constructor
}//endclass TextClock
5
  • 2
    What do you mean it doesn't execute? Does it compile? What is the error.
    – Sid
    Mar 2, 2012 at 3:29
  • It seems to work for me. How are you compiling the file?
    – breen
    Mar 2, 2012 at 3:33
  • You need to describe how you're running it, and what the error is. That's quite an if statement you have there. Mar 2, 2012 at 3:40
  • Hour 17 is 1A.M.? Really? And don't you think you could do the conversion more... Uhm... Programatically, instead of a huge series of if statements?
    – Borealid
    Mar 2, 2012 at 3:53
  • i am exporting it with eclipse, right click the class file, click export, and then jar. im sorry if im getting it wrong, im new at this :\. Mar 2, 2012 at 4:54

1 Answer 1

2

Making the Jar

Export as a runnable jar

enter image description here enter image description here

If you are successful in making a jar then follow the next step.

Running it from console

Method 1

java [-options] -jar jarfile [args...]

Note: Where [-options] are the arguments for the JVM and [args...] is for your jar

Also this will only work if your jar manifest has an entry for main class like this:

Manifest-Version: 1.0
Class-Path: .
Main-Class: CopyOftheclock

Method 2

set your class path like this:

set classpath=clock.jar;.;%classpath%

and after this:

java [-options] class [args...]

Where class is your class with main method. Also class should be fully qualified i.e. if it is in package a.b.c then the class should be a.b.c.CopyOftheclock. Also, you should be firing the java command from the topmost packages parent.

Method 3

I am not sure about *nix based OS but on windows just right click on the jar and select run with java or javaw. But for that the manifest file must have the main class entry, otherwise it would fail.

Note: See the java -help for more details on [options]

2
  • ohh, i know what happned, i dont have the option to do that :( Mar 2, 2012 at 5:22
  • what options ?? please be more specific, that would help in solving your issue.
    – Favonius
    Mar 2, 2012 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.