晚安。 我知道这种类型的错误很常见,我已经尝试搜索它,但没有一个解决方案能解决我的问题。
所以我有一个我想运行的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/