Java 窗口 "could not find or load main class"

标签 java groovy jar

晚安。 我知道这种类型的错误很常见,我已经尝试搜索它,但没有一个解决方案能解决我的问题。

所以我有一个我想运行的jar,我尝试了java -jar rembrandt.jar并得到了问题“无法找到或加载主类rembrandt.bin.Rembrandt”,这个jar有一系列依赖项,如json,我在一个名为lib的文件夹中拥有所有依赖项,所以我尝试了java -cp path -jar rembrandt.jar同样的错误。

我提取了 jar,该类存在于正确的文件夹中。我还可以访问代码并确认该类有一个主要功能(这个类很不错,但我认为这不应该干扰)。

我认为可能是依赖关系,有什么方法可以确认这一点吗?如果是的话,我可以知道每一个吗?

 package rembrandt.bin

 import rembrandt.obj.*
 import saskia.bin.Configuration
 import rembrandt.io.*
 import org.apache.log4j.*
 import org.apache.commons.cli.*
 import java.util.jar.Manifest
 import java.util.jar.Attributes


  class Rembrandt {

    /**
     * Main method.
     */
    static void main(args) {

    def rembrandt, conf, conffilepath
    Options o = new Options()
    o.addOption("conf", true, "Configuration file")
    o.addOption("gui", true, "Activates a graphic GUI")
    o.addOption("help", false, "Gives this help information")

    CommandLineParser parser = new GnuParser()
    CommandLine cmd = parser.parse(o, args)

    if (cmd.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp( "java rembrandt.bin.Rembrandt", o )
        System.exit(0)
    }

    if (!cmd.hasOption("conf")) {
        conffilepath = Configuration.defaultconf
        log.info "No configuration file given. Using default configuration file."
    } else {
        conffilepath = cmd.getOptionValue("conf")
        log.info "Configuration file $conffilepath given."
    }

    conf = Configuration.newInstance(conffilepath)
    rembrandt = new Rembrandt(conf)
    log.info "Rembrandt version ${Rembrandt.getVersion()}. Welcome."

    if (cmd.hasOption("gui")) {

        RembrandtGui gui = new RembrandtGui(rembrandt, conf)//)
         gui.start()
        log.info "Rembrandt GUI started."

    } else {

        log.info "Invoking reader ${rembrandt.inputreader.class.name} to parse the input stream."  

        List<Document> docs = rembrandt.loadDocuments()
        log.info "Got ${docs.size()} doc(s). "

        // give labels if the doc does not have...
        String docid_header
        if (rembrandt.inputFileName) 
            docid_header = rembrandt.inputFileName 
        else 
            docid_header = 'stdin'

        rembrandt.printHeader()

        /* stats stuff */
        def stats = new DocStats(docs.size())
        stats.begin()
        docs.eachWithIndex { doc, i->
        if (!doc.docid) doc.docid = docid_header+"-"+(i+1)
        stats.beginDoc(doc.docid)
        doc = rembrandt.releaseRembrandtOnDocument(doc)
        rembrandt.printDoc(doc)
        stats.endDoc()                                
        stats.printMemUsage()     
        }
        stats.end()
        rembrandt.printFooter()
        log.info "All Done. Have a nice day."
   }
    }
}

此代码是主类的一部分,您可以看到它有一个主类,而包是正确的。 感谢您的支持

最佳答案

您需要创建 MANIFEST 文件。

1)创建manifest.txt

2) 在manifest.txt中,您应该添加以下行:Main-Class: Rembrandt 然后按两次“Enter”按钮。

3) 通过命令提示符创建 jar 文件:# jar cfm Your_Jar_file_name.jar manifest.txt

另外,不要忘记在项目中添加 JAR 文件(确保它们都在一个包(文件夹)中

)

关于Java 窗口 "could not find or load main class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711981/

相关文章:

testing - 是否可以将 easyb 的 Groovy 与简单的英语场景定义分开?

Groovy:有没有在参数拷贝后调用的构造函数?

java - 下载最新的jar文件

javascript - 如何使用cypress手动上传文件?

java - 带 JIT 和不带 JIT 的 JVM 之间的区别

grails - 与namedQuery以多对多关系获取随机记录

java - 如何替换 Java 9 中的认可目录?

java - 在 Linux Mint 中运行 .jar 文件时出现阻止错误

java - 您如何访问传递给 CompletableFuture allOf 的已完成 future ?

java - 为什么用 Java 编写 Excel 时会发生这种情况