2017-08-18 80 views
-1

我在Eclipse工作區導入了this項目。我試圖運行該項目,但它說我不可能找到或加載主類,但有 a MainClass無法在eclipse中導入項目:「無法找到或加載主類」

這是MainClass的代碼:

package com.yeokhengmeng.docstopdfconverter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import org.kohsuke.args4j.CmdLineException; 
import org.kohsuke.args4j.CmdLineParser; 
import org.kohsuke.args4j.Option; 



public class MainClass{ 


    public static final String VERSION_STRING = "\nDocs to PDF Converter Version 1.7 (8 Dec 2013)\n\nThe MIT License (MIT)\nCopyright (c) 2013-2014 Yeo Kheng Meng"; 
    public enum DOC_TYPE { 
     DOC, 
     DOCX, 
     PPT, 
     PPTX, 
     ODT 
    } 

    public static void main(String[] args){ 
     Converter converter; 

     try{ 
      converter = processArguments(args); 
     } catch (Exception e){ 
      System.out.println("\n\nInput\\Output file not specified properly."); 
      return; 
     } 


     if(converter == null){ 
      System.out.println("Unable to determine type of input file."); 
     } else { 
      try { 
       converter.convert(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

    } 


    public static Converter processArguments(String[] args) throws Exception{ 
     CommandLineValues values = new CommandLineValues(); 
     CmdLineParser parser = new CmdLineParser(values); 

     Converter converter = null; 
     try { 
      parser.parseArgument(args); 

      boolean version = values.version; 

      if(version){ 
       System.out.println(VERSION_STRING); 
       System.exit(0); 
      } 


      String inPath = values.inFilePath; 
      String outPath = values.outFilePath; 
      boolean shouldShowMessages = values.verbose; 


      if(inPath == null){ 
       parser.printUsage(System.err); 
       throw new IllegalArgumentException(); 
      } 

      if(outPath == null){ 
       outPath = changeExtensionToPDF(inPath); 
      } 


      String lowerCaseInPath = inPath.toLowerCase(); 

      InputStream inStream = getInFileStream(inPath); 
      OutputStream outStream = getOutFileStream(outPath); 

      if(values.type == null){ 
       if(lowerCaseInPath.endsWith("doc")){ 
        converter = new DocToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       } else if (lowerCaseInPath.endsWith("docx")){ 
        converter = new DocxToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       } else if(lowerCaseInPath.endsWith("ppt")){ 
        converter = new PptToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       } else if(lowerCaseInPath.endsWith("pptx")){ 
        converter = new PptxToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       } else if(lowerCaseInPath.endsWith("odt")){ 
        converter = new OdtToPDF(inStream, outStream, shouldShowMessages, true); 
       } else { 
        converter = null; 
       } 


      } else { 

       switch(values.type){ 
       case DOC: converter = new DocToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       break; 
       case DOCX: converter = new DocxToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       break; 
       case PPT: converter = new PptToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       break; 
       case PPTX: converter = new PptxToPDFConverter(inStream, outStream, shouldShowMessages, true); 
       break; 
       case ODT: converter = new OdtToPDF(inStream, outStream, shouldShowMessages, true); 
       break; 
       default: converter = null; 
       break; 

       } 


      } 

     } catch (CmdLineException e) { 
      // handling of wrong arguments 
      System.err.println(e.getMessage()); 
      parser.printUsage(System.err); 
     } 

     return converter; 

    } 


    public static class CommandLineValues { 

     @Option(name = "-type", aliases = "-t", required = false, usage = "Specifies doc converter. Leave blank to let program decide by input extension.") 
     public DOC_TYPE type = null; 


     @Option(name = "-inputPath", aliases = {"-i", "-in", "-input"}, required = false, metaVar = "<path>", 
       usage = "Specifies a path for the input file.") 
     public String inFilePath = null; 


     @Option(name = "-outputPath", aliases = {"-o", "-out", "-output"}, required = false, metaVar = "<path>", 
       usage = "Specifies a path for the output PDF.") 
     public String outFilePath = null; 

     @Option(name = "-verbose", aliases = {"-v"}, required = false, usage = "To see intermediate processing messages.") 
     public boolean verbose = false; 

     @Option(name = "-version", aliases = {"-ver"}, required = false, usage = "To view version code.") 
     public boolean version = false; 


    } 

    //From http://stackoverflow.com/questions/941272/how-do-i-trim-a-file-extension-from-a-string-in-java 
    public static String changeExtensionToPDF(String originalPath) { 

//  String separator = System.getProperty("file.separator"); 
     String filename = originalPath; 

//  // Remove the path upto the filename. 
//  int lastSeparatorIndex = originalPath.lastIndexOf(separator); 
//  if (lastSeparatorIndex == -1) { 
//   filename = originalPath; 
//  } else { 
//   filename = originalPath.substring(lastSeparatorIndex + 1); 
//  } 

     // Remove the extension. 
     int extensionIndex = filename.lastIndexOf("."); 

     String removedExtension; 
     if (extensionIndex == -1){ 
      removedExtension = filename; 
     } else { 
      removedExtension = filename.substring(0, extensionIndex); 
     } 
     String addPDFExtension = removedExtension + ".pdf"; 

     return addPDFExtension; 
    } 


    protected static InputStream getInFileStream(String inputFilePath) throws FileNotFoundException{ 
     File inFile = new File(inputFilePath); 
     FileInputStream iStream = new FileInputStream(inFile); 
     return iStream; 
    } 

    protected static OutputStream getOutFileStream(String outputFilePath) throws IOException{ 
     File outFile = new File(outputFilePath); 

     try{ 
      //Make all directories up to specified 
      outFile.getParentFile().mkdirs(); 
     } catch (NullPointerException e){ 
      //Ignore error since it means not parent directories 
     } 

     outFile.createNewFile(); 
     FileOutputStream oStream = new FileOutputStream(outFile); 
     return oStream; 
    } 
} 

有這個類沒有錯誤,沒有在其他類項目。

回答

1

因爲它根本就沒有包含你的類在打包的罐子裏。

由於此項目使用Maven。你可以在pom.xml中看到所有的依賴關係(jar)。

https://github.com/yeokm1/docs-to-pdf-converter/blob/master/docs-to-pdf-converter/pom.xml

只需導入項目從您的IDE行家。

mvn clean install 

樣本輸出從Eclipse或Maven的正常運行REPL:

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 

Results : 

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ docs-to-pdf-converter --- 
[INFO] Building jar: /home/docs-to-pdf-converter/target/docs-to-pdf-converter-1.8.jar 
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ docs-to-pdf-converter --- 
[INFO] Installing /home/docs-to-pdf-converter/target/docs-to-pdf-converter-1.8.jar to /home/**/.m2/repository/docs-to-pdf-converter/docs-to-pdf-converter/1.8/docs-to-pdf-converter-1.8.jar 
[INFO] Installing /home/docs-to-pdf-converter/pom.xml to /home/**/.m2/repository/docs-to-pdf-converter/docs-to-pdf-converter/1.8/docs-to-pdf-converter-1.8.pom 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.346s 
[INFO] Finished at: Fri Aug 18 06:18:31 EDT 2017 
[INFO] Final Memory: 23M/475M 
[INFO] ------------------------------------------------------------------------ 

我的環境:

java : 1.8.0_141 and maven : Apache Maven 3.0.5 
+0

我已經試過您的解決方案,但當我導入項目總有一些類,埃奇無法找到... – Clyky

+0

我使用java:1.8.0_141和maven:Apache Maven 3.0.5。我能夠使用eclipse和普通的maven REPL來構建這個項目。 – Abhi

+0

非常感謝,我一直遵循你的解決方案,也感謝Google解決一些其他小問題,我管理這個項目運行完美:) – Clyky

0

試試這個, 文件 - >導入 - > Maven和選擇「文檔到PDF轉換器」

0

如果它告訴你是不可能找到或加載主類,這意味着你沒有主在你的班級。可能還有其他解釋,但是如果沒有看到您的代碼,我們就無法打電話。

我們不會爲您檢查所有github項目,給我們提供我們需要的信息。

+0

我已經包含主類代碼 – Clyky

相關問題