0

Problem that I am facing right now is that I want import an excel file and pass it to another class like this:

package cal;

import java.util.Scanner;

public class CalendarMain {
public static void main(String[] args) {

    Scanner link = new Scanner(System.in);
    System.out.println("Please insert the raw file link: ");
    String excellink = link.nextLine();
    link.close();

    String[] excelLink = new String[] { excellink };
    ShiftSetting.main(excelLink);
   }
}

There was no problem for compile and run it through command prompt but when paste the raw file link into the command prompt and click enter, it give me this error:

C:\Users\User\eclipse-workspace\Calendar_v1\src>java cal.CalendarMain cal.ShiftSetting cal.PHNames
    Please insert the raw file link:
    C:\Users\User\Desktop\WORK\2019raw.xlsx
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
            at cal.CalendarMain.main(CalendarMain.java:16)
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Row
            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)
            ... 1 more

FYI, this problem never arise when I run it on Eclipse. But when I try run on command prompt, it give me this error. Line 16 is actually this line : ShiftSetting.main(excelLink);

I provide also the ShiftSetting.java file

package cal;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ShiftSetting {
    public static void main(String[] excelLink) {
        ArrayList<String> shiftname = new ArrayList<String>();
        ArrayList<String> shiftcode = new ArrayList<String>();
        try {
            FileInputStream file = new FileInputStream(new File("C:\\Users\\User\\Desktop\\WORK\\shiftnameall.xlsx"));
            XSSFWorkbook wb = new XSSFWorkbook(file);
            XSSFSheet sheet = wb.getSheetAt(0);
            DataFormatter formatter = new DataFormatter();
            String cellValue = null;
            for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
                Row r = sheet.getRow(rowNum);
                String val = null;
                for (int m = 0; m < r.getLastCellNum(); m = m + 2) {
                    Cell cell = r.getCell(m);
                    val = formatter.formatCellValue(cell);
                    shiftname.add(val);
                }
            }
            for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
                Row r = sheet.getRow(rowNum);

                String val = null;
                for (int m = 1; m < r.getLastCellNum(); m++) {
                    Cell cell = r.getCell(m);
                    val = formatter.formatCellValue(cell);
                    shiftcode.add(val);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* PHDatesArray.main(excelLink, shiftname, shiftcode); */
    }
}

Anyone has idea how to solve this problem?

sharky
  • 196
  • 1
  • 1
  • 14
  • 3
    There is two `public static void main (String[])` methods, JVM getting confused which `main` method to call. Try changing the name of `main` in `ShiftSetting` class. – Zain Ul Abideen Dec 18 '18 at 05:39
  • Make sure that you have apache poi dependencies on classpath – Humza Dec 18 '18 at 05:44
  • @ZainUlAbideen I change it to`main2` but it still give me the same error – sharky Dec 18 '18 at 05:48
  • @Humza yes, when I compile it I write like this `javac -cp jars/dom4j-1.6.1.jar;jars/poi-3.7.jar;jars/poi-examples-3.7.jar;jars/poi-ooxml-3.7.jar;jars/poi-ooxml-schemas-3.7.jar;jars/poi-scratchpad-3.7.jar;jars/xmlbeans-2.3.0.jar CalendarMain.java ShiftSetting.java PHNames.java` – sharky Dec 18 '18 at 05:49
  • have you loaded jar files on classpath? – Aravind P Dec 18 '18 at 05:51
  • @AravindP you mean write like this ? `javac -cp jars/dom4j-1.6.1.jar;jars/poi-3.7.jar;jars/poi-examples-3.7.jar;jars/poi-ooxml-3.7.jar;jars/poi-ooxml-schemas-3.7.jar;jars/poi-scratchpad-3.7.jar;jars/xmlbeans-2.3.0.jar CalendarMain.java ShiftSetting.java PHNames.java` ? – sharky Dec 18 '18 at 05:52
  • 1
    use `:` instead of `;` https://stackoverflow.com/a/2096298/6223839 – Humza Dec 18 '18 at 05:52
  • @sharky Yes, Try it. – Aravind P Dec 18 '18 at 05:54
  • @Humza may I know what is dir1 stands for from `java -cp jar1:jar2:jar3:dir1:. HelloWorld` – sharky Dec 18 '18 at 06:03
  • @sharky it is symbolic for directory path for jar that are not in current directory – Humza Dec 18 '18 at 06:16
  • as for your suggestion by using `:` instead of `;`, I think for window user, I should use `;`? @Humza – sharky Dec 18 '18 at 06:40
  • might be jar missing from the class path; add `org/apache/poi/ss/usermodel/Row` to your classpath. – Zain Ul Abideen Dec 18 '18 at 07:28
  • @ZainUlAbideen you mean, i add `org/apache/poi/ss/usermodel/Row` into here ? `javac -cp jars/dom4j-1.6.1.jar;jars/poi-3.7.jar;jars/poi-examples-3.7.jar;jars/poi-ooxml-3.7.jar;jars/poi-ooxml-schemas-3.7.jar;jars/poi-scratchpad-3.7.jar;jars/xmlbeans-2.3.0.jar CalendarMain.java ShiftSetting.java ` – sharky Dec 18 '18 at 07:58
  • Why are you using such an old version of Apache POI? 3.7 is ancient and [there have been huge numbers of fixes in the last *8* years!](http://poi.apache.org/changes.html#3.7) – Gagravarr Dec 18 '18 at 07:59
  • I will try change to latest version and will update the status soon @Gagravarr – sharky Dec 18 '18 at 08:34
  • Specify the class path to `java` exactly the same way you specified it to `javac`. – Holger Dec 18 '18 at 09:55
  • @Holger I did like this `java -cp jars/dom4j-1.6.1.jar;jars/poi-3.7.jar;jars/poi-3.2-final.jar;jars/poi-examples-3.7.jar;jars/poi-ooxml-3.7.jar;jars/poi-ooxml-schemas-3.7.jar;jars/poi-scratchpad-3.7.jar;jars/xmlbeans-2.3.0.jar:dir1;. cal.CalendarMain cal.ShiftSetting ` when run it. but I said, when I enter the `raw file link` it give me the error as I stated that which is `Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row` – sharky Dec 18 '18 at 23:32
  • This is not the same classpath you used for `javac`. Most notably, you have an even more ancient poi **3.2** in your class path, together with the 3.7 version. It’s not necessary to explain why mixing different versions of the same library is a bad idea, is it? – Holger Dec 19 '18 at 12:22

1 Answers1

0

I have solved this problem. Actually this is not related to the version of APACHE POI, or the PATH inside my system variable. it just my path on my command prompt that I write it wrongly.

I have put all my .jar files inside a folder which inside the the same folder package.

Hence I run the command to compile like this:

javac -cp jars/*;. *.java

then to run it, I write like this

java -cp calendar/jars/*; calendar.nameofclass1, calendar.nameofclass2

sharky
  • 196
  • 1
  • 1
  • 14