简体   繁体   English

无法将IntelliJ IDEA程序编译为可运行的JAR

[英]Cannot compile IntelliJ IDEA program to runnable JAR

I am working on a school project where I am writing a Scala class to be part of a Java project, using IntelliJ IDEA on MacOS. 我正在做一个学校项目,在MacOS上使用IntelliJ IDEA,我正在编写一个Scala类作为Java项目的一部分。 Here is what the folder structure looks like: 文件夹结构如下所示:

TopLevelFolderName
  .idea
  .settings
  bin
  classes
    artifacts
    TopLevelFolderName.jar
  src
    dir1
      JavaClass.java
      Main.java
    dir2
      ScalaClass.scala
    META-INF
    .classpath
    .project
    TopLevelFolderName.iml

I can build and run the program to run the main function in the Main class, using the Scala code in ScalaClass . 我可以使用ScalaClass的Scala代码构建并运行程序以运行Main类中的main函数。

However, when I set up the project structure to build a JAR in the classes/artifacts folder, the JAR file doesn't work when I go to the menu and click Run TopLevelFolderName.jar . 但是,当我将项目结构设置为在classes/artifacts文件夹中构建JAR时,当我转到菜单并单击Run TopLevelFolderName.jar时,JAR文件不起作用。 I get the error: 我得到错误:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Tuple2

Does anyone know how to set up my project such that Scala standard dependencies get included and are made accessible to the JAR file? 有谁知道如何设置我的项目,以使Scala标准依赖项包含在内并使JAR文件可访问? This is my first time using IntelliJ IDEA. 这是我第一次使用IntelliJ IDEA。

Thank you, and please let me know if I can provide more information. 谢谢,如果可以提供更多信息,请告诉我。

The problem is that your JAR has dependencies, including the Scala runtime, which need to be passed to java at runtime with the -classpath argument. 问题是您的JAR具有依赖关系,包括Scala运行时,需要在运行时使用-classpath参数将其传递给java

If you click "play" in IntelliJ to run your project, then you will see the java command that IntelliJ used at the start of the output (click on the "..." to expand it if neccessary). 如果单击IntelliJ中的“播放”以运行您的项目,那么您将在输出的开头看到IntelliJ使用的java命令(必要时单击“ ...”以将其展开)。 You will notice that the command includes a -classpath argument with a very long list of required JARs. 您会注意到该命令包括-classpath参数,其中包含非常长的必需JAR列表。 You could copy & paste this command into your shell to run the JAR if you wanted. 您可以根据需要将此命令复制并粘贴到您的外壳中以运行JAR。

I think what you are probably looking for is how to create a single JAR which includes all your dependencies, which is described here: How to build jars from IntelliJ properly? 我认为您可能正在寻找的是如何创建一个包含所有依赖项的JAR,如此处所述: 如何从IntelliJ正确构建jars?

(You can also avoid this issue by running the project from IntelliJ, rather than from a JAR, depending on what you are trying to achieve.) (您也可以通过从IntelliJ而不是JAR运行项目来避免此问题,具体取决于您要实现的目标。)

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

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