java - 无法找到或加载主类 cmd,它在 Eclipse 中工作正常,构建路径似乎没问题

标签 java jar cmd javac jexcelapi

大家好,感谢您的尝试 我正在使用 JExcelApi 从 cmd 提示符和 java 文件创建 Excel 文件。

代码在 Eclipse 中完美运行,具有正确的构建路径等。 当我在 cmd 提示符下尝试时,javac 起作用,然后当我执行它时,我得到无法找到或加载主类GenerateExcel

这是GenerateExcel Java 文件

  import java.io.*;
  import jxl.*;
  import java.util.*;
  import jxl.Workbook;
  import jxl.write.DateFormat;
  import jxl.write.Number;

  import jxl.write.*;
  import java.text.SimpleDateFormat;

  public class  GenerateExcel
  {
    public static void main(String[] args) 
    {
      try
      {
        String filename = "excelTEST.xls";
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook = 
          Workbook.createWorkbook(new File(filename), ws);
        WritableSheet s = workbook.createSheet("Sheet1", 0);
        WritableSheet s1 = workbook.createSheet("Sheet1", 0);
        writeDataSheet(s);
        writeImageSheet(s1);
        workbook.write();
        workbook.close();      
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
      catch (WriteException e)
      {
        e.printStackTrace();
      }
    }

    private static void writeDataSheet(WritableSheet s) 
      throws WriteException
    {

      /* Format the Font */
      WritableFont wf = new WritableFont(WritableFont.ARIAL, 
        10, WritableFont.BOLD);
      WritableCellFormat cf = new WritableCellFormat(wf);
      cf.setWrap(true);

      /* Creates Label and writes date to one cell of sheet*/
      Label l = new Label(0,0,"Date",cf);
      s.addCell(l);
      WritableCellFormat cf1 = 
        new WritableCellFormat(DateFormats.FORMAT9);

      DateTime dt = 
        new DateTime(0,1,new Date(), cf1, DateTime.GMT);

      s.addCell(dt);

      /* Creates Label and writes float number to one cell of sheet*/
      l = new Label(2,0,"Float", cf);
      s.addCell(l);
      WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
      Number n = new Number(2,1,3.1415926535,cf2);
      s.addCell(n);

      n = new Number(2,2,-3.1415926535, cf2);
      s.addCell(n);

      /* Creates Label and writes float number upto 3 
         decimal to one cell of sheet */
      l = new Label(3,0,"3dps",cf);
      s.addCell(l);
      NumberFormat dp3 = new NumberFormat("#.###");
      WritableCellFormat dp3cell = new WritableCellFormat(dp3);
      n = new Number(3,1,3.1415926535,dp3cell);
      s.addCell(n);

      /* Creates Label and adds 2 cells of sheet*/
      l = new Label(4, 0, "Add 2 cells",cf);
      s.addCell(l);
      n = new Number(4,1,10);
      s.addCell(n);
      n = new Number(4,2,16);
      s.addCell(n);
      Formula f = new Formula(4,3, "E1+E2");
      s.addCell(f);

      /* Creates Label and multipies value of one cell of sheet by 2*/
      l = new Label(5,0, "Multipy by 2",cf);
      s.addCell(l);
      n = new Number(5,1,10);
      s.addCell(n);
      f = new Formula(5,2, "F1 * 3");
      s.addCell(f);

      /* Creates Label and divide value of one cell of sheet by 2.5 */
      l = new Label(6,0, "Divide",cf);
      s.addCell(l);
      n = new Number(6,1, 12);
      s.addCell(n);
      f = new Formula(6,2, "F1/2.5");
      s.addCell(f);
    }

    private static void writeImageSheet(WritableSheet s) 
      throws WriteException
    {
      /* Creates Label and writes image to one cell of sheet*/    
      Label l = new Label(0, 0, "Image");
      s.addCell(l);
      WritableImage wi = new WritableImage(0, 3, 5, 7, new File("C:/3DWIP/out/example.png"));
      s.addImage(wi);

      /* Creates Label and writes hyperlink to one cell of sheet*/
      l = new Label(0,15, "HYPERLINK");
      s.addCell(l);
      Formula f = new Formula(1, 15, 
        "HYPERLINK(\"http://www.andykhan.com/jexcelapi\", "+
        "\"JExcelApi Home Page\")");
      s.addCell(f);

      }
     }

名为 jxl.jar 的 JExcelApi jar 位于 c:/3DWIP/src/api

 cd..
 cd 3DWIP
 cd src

所以我终于加入了

 C:\3DWIP\src

我的GenerateExcel.java 文件在哪里

我用cmd编译它

 javac -classpath ./api/* GenerateExcel.java

一切顺利,然后当我用cmd执行它时:

 java -classpath ./api/* GenerateExcel

然后我收到错误

 Could not find or load main class GenerateExcel

谢谢大家

最佳答案

-classpath 覆盖您当前的类路径。

将包含具有 main() 方法的类的目录位置添加到环境变量中的路径变量

关于java - 无法找到或加载主类 cmd,它在 Eclipse 中工作正常,构建路径似乎没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26659205/

相关文章:

java - 将对象的 ArrayList 转换为此对象的 ArrayList Java 中的字符串名称

java - 如何让 "run"按钮自动启动指定配置的项目

Java:打开 Jar 内的 xml 文件。打不开

batch-file - 如何在cmd中重定向输入

java - C++ 的 const 成员函数的 Java 等价物是什么?

java - Android/Java - 单击 ListView 中的图像

java - 使用外部 jar 运行编译的程序时出错

mysql - 在 $catalina_home/lib 中带有 mysql 驱动程序的 maven tomcat 插件

linux - STDOUT 不能通过 Groovy API 以编程方式工作

c++ - 在 C++ 中运行 CMD 行使用变量(不是字符串文字)作为参数