-1

I am writting a program with netbeans and create a .jar file. Here is my main class:

public class Test {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //try {
    // TODO code application logic here

    //create an object and execute the function to calculate monthly wage
    CalculateMonthlyWage monthlyWage = new CalculateMonthlyWage();
    monthlyWage.CalculateDailyWorkingDurationFromCSV();

    //round the result to 2 decimal
    BigDecimal result1 = round(monthlyWage.getTotalMonthID1(),2);
    BigDecimal result2 = round(monthlyWage.getTotalMonthID2(),2);
    BigDecimal result3 = round(monthlyWage.getTotalMonthID3(),2);
    System.out.println("1, Janet Java, $"+result1);
    //create Java swing window
    Frame f = new Frame();
    JLabel mLabel = new JLabel();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    mLabel.setText(convertToMultiline("Monthly Wage 3/2014"
            +"\n1, Janet Java, $"+result1.toString()
            +"\n2, Scott Scala, $"+result2.toString()
            +"\n3, Larry Lolcode, $"+result3.toString()));
    f.add(mLabel);
    f.setSize(500, 500);
    f.setVisible(true);
}    
public static String convertToMultiline(String orig) {
    return "<html>" + orig.replaceAll("\n", "<br>");
}
}

This class is the main, which will create a CalculateMonthlyWage class object and then call the CalculateDailyWorkingDurationFromCSV() method. After that it will create a window and display the result.

When I tried running with the IDE, everthing was fine and the java window displayed what I want. But when I clicked the .jar file, nothing happened.

When I try to run it from the command line using java -jar jarname.jar, I get the following error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant

What am I doing wrong here?

EDIT

I downloaded Eclipse, created a new project and copy my source code to it. Everything is fine now. Still I dont understand what's wrong with Netbeans

Tuan Dinh
  • 407
  • 1
  • 3
  • 13
  • 1
    Is there a MANIFEST in your jar, that would tell Java which is the main class? https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html – dotvav Aug 20 '15 at 10:29
  • 1
    possible duplicate of [How to setup Main class in manifest file in jar produced by NetBeans project](http://stackoverflow.com/questions/2848642/how-to-setup-main-class-in-manifest-file-in-jar-produced-by-netbeans-project) – dotvav Aug 20 '15 at 10:30
  • I have a manifest and I set main class also – Tuan Dinh Aug 20 '15 at 10:38
  • possible duplicate of [Setting classpath for a JAR](http://stackoverflow.com/questions/8789683/setting-classpath-for-a-jar) – fabian Aug 20 '15 at 10:55
  • @fabian: I have read and edit my manifest and test before I ask this question – Tuan Dinh Aug 20 '15 at 11:03

3 Answers3

0

Maybe you haven't compiled your program as an executable JAR file.

Nicolas
  • 131
  • 2
0

you can always check the exception that is being thrown by opening a command prompt , navigate to the directory where the jar is and call java -jar yourJar.jar. If the application's main called up properly , then the application will start up , or else it will throw an exception , in order to better understand the issue. Now because you are using netbeans to export to jar , netbeans do not include the libraries inside the jar but instead it adds in the manifest the location in which the libraries can be found, so probably the issue must be that case , otherwise try the first step i told you and share the exception.

AntJavaDev
  • 1,204
  • 1
  • 18
  • 24
  • I check the exception and it said: Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant – Tuan Dinh Aug 20 '15 at 10:42
  • then you dont have the libraries in the classpath , try to find the output directory in which NetBeans is placing the jar , and you will see a folder there Named lib . So in order to hit the jar and play , you have to place the lib folder in the same directory of the jar – AntJavaDev Aug 20 '15 at 10:55
  • I have the folder lib in the same class path with the jar. Both of them is in the dist folder. It is like root---dist/src/build/nbproject/manifest.mf---lib and jar – Tuan Dinh Aug 20 '15 at 10:56
  • is joda-time jar in the library folder? – AntJavaDev Aug 20 '15 at 11:00
  • so the paths are like `../dist/lib` and `../dist/yourJar.jar` – AntJavaDev Aug 20 '15 at 11:05
  • also another comment , hope that the java version you are using in NetBeans , is the same with the system's java version – AntJavaDev Aug 20 '15 at 11:13
  • The path is like that and it's java 8 I tried this way, copy and move all the library jar and the manifest and my two java class in the same folder. I created a jar with console command but this time, it is not running, not even a window appear when clicked on it – Tuan Dinh Aug 20 '15 at 11:16
  • well as a last advice , if you can post your manifest.xml to check the path that is trying to load the libraries , and if you are familiar with maven build tools i would suggest you to change a bit your project , in order to use maven builder , because it will place the libraries inside the jar – AntJavaDev Aug 20 '15 at 12:58
  • I download the eclipse and create a new project. Everything is fine now. – Tuan Dinh Aug 20 '15 at 14:01
0

You have to compile it as an executable Jar File. You can also compile it manually with CMD, move to your directory and compile or run it:

Compiling

javac filename.java

Executing

java filename
pguetschow
  • 5,176
  • 6
  • 32
  • 45