0

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. 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.

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. 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? This is my first time using IntelliJ IDEA.

Thank you, and please let me know if I can provide more information.

dkim
  • 3,930
  • 1
  • 33
  • 37

1 Answers1

1

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.

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). You will notice that the command includes a -classpath argument with a very long list of required JARs. You could copy & paste this command into your shell to run the JAR if you wanted.

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?

(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.)

Rich
  • 15,048
  • 2
  • 66
  • 119