0

I am relatively new to java. I am writing a piece of code that will work with excels. For that purpose i am using jExcel API.

Now my code is like this.

import java.io.File;
import jxl.*;
class main{
 private File outputFile;
 private String  destinationFolder;

 public  main(){
    this.destinationFolder="test";
 }
 public void fileIteration(){
    try{
        for(File fileEntry :  new File(this.destinationFolder).listFiles()){

            if (fileEntry.isDirectory()){

            }
            else{
                System.out.println(fileEntry.getName());
                this.excelManipulate(fileEntry.getName());
            }
        }
    }

    catch(Exception e){
        e.printStackTrace();
    }
 }


 public void excelManipulate(String inputFile){
 try{
     System.out.println(inputFile);
     Workbook workbook=Workbook.getWorkbook(new File("test/"+inputFile));
     Sheet sheet=workbook.getSheet(0);
     Cell a5=sheet.getCell("a1");
     System.out.println(a5.getContents());;
 }
 catch(Exception e){
    System.out.println(e.getMessage());
 }
 }

 public static void  main(String[] args){
    main ofh=new main();
    ofh.fileIteration();
    }
 }

I have a few excels sitting at D:\java\Automation\src\test.

Now i am using command prompt of windows to compile and run the code.

the compiling command i am giving is
javac -cp "D:\java\automation\jexcelapi\jxl.jar" main.java and then I am running the command java main While the code compiles correctly, its giving the following error while running.

D:\java\Automation\src>java main
 as307d.xls
 as307d.xls
 Exception in thread "main" java.lang.NoClassDefFoundError: jxl/Workbook
         at main.excelManipulate(main.java:37)
         at main.fileIteration(main.java:23)
         at main.main(main.java:50)
 Caused by: java.lang.ClassNotFoundException: jxl.Workbook
         at java.net.URLClassLoader$1.run(Unknown Source)
         at java.net.URLClassLoader$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         ... 3 more

When I am running it with the jar explicitly mentioned I am getting the following error.

 D:\java\Automation\src>java -jar D:\java\automation\jexcelapi\jxl.jar main
 java.io.FileNotFoundException: main (The system cannot find the file specified)
 java.io.FileNotFoundException: main (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at jxl.Workbook.getWorkbook(Workbook.java:213)
        at jxl.Workbook.getWorkbook(Workbook.java:198)
        at jxl.demo.Demo.main(Demo.java:276)

I am not sure why i am not getting the file names printed atleast. Please let me know where i have done the mistake and if there is any problem with the code. Thanks a lot.

Roy
  • 15
  • 3

2 Answers2

0

The -jar option is used to run executable jar files. Use the -classpath flag instead.

java -classpath D:\java\automation\jexcelapi\jxl.jar;. main

Also note that Java class names start with an uppercase letter, e.g. Main

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

You have to add the jxl.jar to your classpath.

Call java -cp D:\java\automation\jexcelapi\jxl.jar;.\ main

Jens
  • 67,715
  • 15
  • 98
  • 113