A small problem in executing scala program with JAVA command line

Posted by jdimino on Mon, 16 Dec 2019 19:38:07 +0100

Example of scala accessing MySQL database:

import java.sql.{Connection,ResultSet,DriverManager}

import scala.util.control.Exception.Catch

//import java.sql.DriverManager
object DataAnalysisTest {
  def main(args: Array[String]): Unit = {
    if(args.length!=3)
      {
        println("Parameter error!")
        println("usage method: DataAnalysisTest Database name user name password")
        return
      }
    //Driver name
  val driver = "com.mysql.jdbc.Driver"
  //Database name
  var dbdata = args(0)
  //User name
  val username = args(1)
  //Password
  val password = args(2)
  
  val dbc = "jdbc:mysql://localhost:3306/"+dbdata+"?user="+username+"&password="+password
  //Initialize data connection
  var connection:Connection =null   
  //
  try
  {
    Class.forName(driver)
    connection = DriverManager.getConnection(dbc) 
    val statement = connection.createStatement()
    val rs = statement.executeQuery("select * from imtesttable ")
    while(rs.next())
    {
      println(rs.getInt("id"))
      println(rs.getInt("f0"))
    }
    
   
    connection.close()
  }
  catch{
    case e:Exception=>e.printStackTrace()
  }
  }  
}

The program executes normally in eclipse. The command line is executed in scala (the above source code is located in C: \ users \ Joe \ eclipse workspace \ helloscala \):

C: \ users \ Joe \ eclipse workspace \ helloscala \ bin > scala dataanalysistest database name user name password

The program can also be executed normally. Now you want to use the java command line to execute:

First, the relevant environment variables are displayed here:

C:\Users\joe\eclipse-workspace\HelloScala\bin>echo %SCALA_HOME%
C:\Program Files (x86)\scala

I'll now start typing commands in cmd:

C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86)
/eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library
.source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";D:\software\mysql-connector-
java-5.1.46/mysql-connector-java-5.1.46.jar;%SCALA_HOME%\lib\*  DataAnalysisTest
 dbtest root 123abc
//Error: cannot find or load main class Files

Then I put the last scala related jar Library in the front:

C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86)
/eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library
.source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";"%SCALA_HOME%\lib\*";D:\soft
ware\mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar  DataAnalysisTe
st dbtest root 123abc
1
10
2
11

Indicates successful execution. How could this happen? Can't the format of xxx \ * jar be put at the back? It should not be. Further experiments are as follows:

C:\Users\joe\eclipse-workspace\HelloScala\bin>java -cp .;"D:/Program Files (x86)
/eclipse-jee-oxygen-3a-win32-x86_64/eclipse/plugins/org.scala-lang.scala-library
.source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar";D:\software\mysql-connector-
java-5.1.46/mysql-connector-java-5.1.46.jar;"%SCALA_HOME%\lib\*"  DataAnalysisTe
st dbtest root 123abc
1
10
2
11

Again, it's just because there are no double quotes.

Topics: Eclipse Scala Java MySQL