How to fix error and what causes: Error: Main method is not static in class

67 Views Asked by At

I am getting this error:

Error: Main method is not static in class oracle.Execution$, please define the main method as: public static void main(String[] args)

When I run the command:

java -jar MyProgram.jar oracle.Execution

But I can't figure out why is it happening. This is my code (very simple)

package oracle

import publish.PublishFile

object Execution {
    def main(args: Array[String]): Unit = {

        val reader = OracleReader()

        val SEPARATOR = System.getProperty("file.separator")
        val END_PATH = s"${System.getProperty("user.dir")}${SEPARATOR}testingDumpResults"
        PublishFile().publishThroughFile(END_PATH, reader)

    }

}

I've also tried this but the same error occurs:

object Execution extends App {
        val reader = OracleReader()

        val SEPARATOR = System.getProperty("file.separator")
        val END_PATH = s"${System.getProperty("user.dir")}${SEPARATOR}testingDumpResults"
        PublishFile().publishThroughFile(END_PATH, reader)

}

Also when I run the program using the InteliJ interface it runs and I can't seem to find what is the cause of the error.

1

There are 1 best solutions below

1
Anubrij Chandra On

it seems like you're defining Execution as a Scala object correctly. However, when you're trying to execute it using the java command with the -jar option, Java expects a Java class with a main method, not a Scala object.

To execute a Scala program packaged in a JAR file, you should use the scala command instead of java. Here's how you can run your Scala program:

scala -cp MyProgram.jar oracle.Execution