简体   繁体   English

使用 Java+Scala+Slick2D 时“无法从给定的启动配置中找到主要方法”

[英]"Could not find main method from given launch configuration" when using Java+Scala+Slick2D

I've got a project in which I'm using Java+Scala+Slick2D.我有一个使用 Java+Scala+Slick2D 的项目。

The project itself runs well when launched from within eclipse.从 eclipse 中启动时,项目本身运行良好。 But when I try to make a jar file, it just refuses to work.但是当我尝试制作 jar 文件时,它只是拒绝工作。 Here's the error I keep getting when trying to export it as a Runnable jar:这是我尝试将其导出为 Runnable jar 时不断收到的错误:

在此处输入图片说明

And if I try to export into just a Jar file, it's unable to find the Main Class:如果我尝试仅导出为 Jar 文件,则无法找到主类:

在此处输入图片说明

There is, of course, a main class in game.TicTacGame.当然,game.TicTacGame 中有一个主类。 But it refuses to acknowledge it.但它拒绝承认。 I tried creating an executable with a simple Hello World project and it worked fine.我尝试用一​​个简单的 Hello World 项目创建一个可执行文件,它运行良好。 It even detects the main class.它甚至可以检测主类。 Why is Eclipse not detecting the main class in this case?为什么 Eclipse 在这种情况下没有检测到主类?

PS: I've also tried extracting the .jar file created, editing the manifest.mf file to add the Main-Class: game.TicTacGame, enter two new lines and recreate the jar. PS:我也试过提取创建的 .jar 文件,编辑 manifest.mf 文件以添加 Main-Class: game.TicTacGame,输入两个新行并重新创建 jar。 Then it gives me a corrupted jar file error.然后它给了我一个损坏的 jar 文件错误。

I'm at my wits end and would appreciate any help in this regard.我不知所措,将不胜感激在这方面的任何帮助。 I'm using Windows 7 x64 with Eclipse Juno, Java 1.7 and Scala 2.10我将 Windows 7 x64 与 Eclipse Juno、Java 1.7 和 Scala 2.10 一起使用

Edit: The Main class is in Java编辑:主类在 Java 中

Okay, I got it to work.好的,我让它工作了。 Apparently, all I needed to do was restart eclipse.显然,我需要做的就是重新启动 eclipse。 And then magically, it started detecting the Main class:然后神奇的是,它开始检测 Main 类:

在此处输入图片说明

But the jar started giving me noClassDefFound errors for the Slick2d, LWJGL and other libraries.但是 jar 开始给我 Slick2d、LWJGL 和其他库的 noClassDefFound 错误。 That's where JarSplice came to my rescue.这就是JarSplice 来救我的地方。 I exported the project as before, with all the libraries and resources.我像以前一样导出了项目,以及所有的库和资源。

Then, I fired up JarSplice and added everything as follows:然后,我启动了 JarSplice 并添加了以下所有内容:

  1. Go to "Add Jars" and add the project.jar just created through eclipse, add lwjgl.jar, slick.jar and scala-library.jar.进入“Add Jars”,添加刚刚通过eclipse创建的project.jar,添加lwjgl.jar、slick.jar和scala-library.jar。 lwjgl and slick should be in your project lib folder where you would have imported them. lwjgl 和 slick 应该位于您将导入它们的项目 lib 文件夹中。 scala-library should be available wherever it says it is. scala-library 应该在它所说的任何地方可用。 Make sure you add that as well确保你也添加了在此处输入图片说明
  2. Next, when I tried to "Add Natives" it kept giving me "Duplicate library" error.接下来,当我尝试“添加本地人”时,它一直给我“重复库”错误。 So I removed them all and kept it empty.所以我把它们都删除了,让它空着。
  3. Next in "Main Class", I entered the path to the main class ie game.TicTacGame接下来在“主类”中,我输入了主类的路径,即 game.TicTacGame
  4. Finally, "Create Fat Jar".最后,“创建胖罐”。 And it works perfectly :)它完美地工作:)

I just encountered the same problem, and here is how I solved it:我刚刚遇到了同样的问题,这是我解决的方法:

  1. Open "Run As" --> "Run Configuration" on the project you want to export在要导出的项目上打开“运行方式”-->“运行配置”

  2. Click "Search" for Eclipse to refresh the list of main class Eclipse点击“搜索”刷新主类列表

  3. Then export Runnable JAR file again, and everything goes smoothly.然后再次导出Runnable JAR文件,一切顺利。

I recently figured a better way to do this using ' Runnable jar export ' which might help you.我最近想出了一个更好的方法来使用“ Runnable jar export ”,这可能会对您有所帮助。 In order for your main method to be listed in that list, you need to add the main method to the Run Configuration list.为了在该列表中列出您的主要方法,您需要将主要方法添加到“运行配置”列表中。

This way it's simpler to create a runnable jar especially if you want to do it repeatedly.这样,创建可运行的 jar 会更简单,尤其是如果您想重复执行此操作。

好吧,遇到了同样的问题并通过选择我的 GUI 来导出而不是整个项目来解决它。

最简单的方法是运行一次 java 文件,该文件会自动出现在列表中。即使我遇到了错误,但使用这个简单的方法就解决了。

I had this problem with Eclipse version 2019-03 (4.11.0) and compiler JaveSE-11.我在 Eclipse 版本 2019-03 (4.11.0) 和编译器 JaveSE-11 上遇到了这个问题。 Choosing the right launch configuration and exporting runnable jar failed over and over again with an error选择正确的启动配置并导出可运行的 jar 一次又一次地失败并出现错误

Could not find main method from given launch configuration无法从给定的启动配置中找到 main 方法

I've tried to restart Eclipse and do a clean build, but it didn't help.我尝试重新启动 Eclipse 并进行干净的构建,但没有帮助。

In the end, I found a workaround to go into the generated jar file (I've used 7zip) and change META-INF/MANIFEST.MF file.最后,我找到了一个解决方法来进入生成的 jar 文件(我使用了 7zip)并更改 META-INF/MANIFEST.MF 文件。 The file should have something like this inside:文件里面应该有这样的东西:

Manifest-Version: 1.0
Class-Path: .
Main-Class: <package name>.<class name>

Hope it helps someone.希望它可以帮助某人。

I faced the same issue and in my case I found that "launch configuration" was incorrect in Runnable Jar file specification dialog.我遇到了同样的问题,就我而言,我发现 Runnable Jar 文件规范对话框中的“启动配置”不正确。 Somehow, eclipse was automatically taking it.不知何故,eclipse 自动接受了它。 The "Launch configuration" should be java file which has main method. “启动配置”应该是具有 main 方法的 java 文件。 Basically, it is filename-package.基本上,它是文件名包。 The moment I changed my "launch configuration", I was able to create Jar file without any error.在我更改“启动配置”的那一刻,我能够毫无错误地创建 Jar 文件。

I had the same issue.我遇到过同样的问题。 To overcome this issue, you need to close all opened files and open a single java class file that has the main() method.要解决这个问题,您需要关闭所有打开的文件并打开一个具有 main() 方法的 java 类文件。 Then export it from eclipse and works fine.然后从 eclipse 导出它并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM