无法将类为'java.lang.String'的对象'url'转换为类'int'。Gradle Java

4

我想读取gradle.properties文件并在build.gradle中使用它。我已经在属性文件中定义了一些参数值,现在想要将这些值传递给参数。这样它就会将此参数值传递给我的主方法。但是我遇到了以下错误:

group 'org.name'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'groovy'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile "joda-time:joda-time:2.2"
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
    // https://mvnrepository.com/artifact/org.dbunit/dbunit
    compile group: 'org.dbunit', name: 'dbunit', version: '2.4.7'
    compile "org.slf4j:slf4j-simple:1.7.9";
}
task runApp(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'ExportDatatoXML'
    println url
    println username
    println password
    println folderPath
    // arguments to pass to the application
    args  
     [project.property('url'),project.property('username'),
      project.property('password'),project.property('folderPath')]
 }

这是我的build.gradle文件。 gradle.properties文件是:
url =jdbc:mysql://127.0.0.1:3306/name
username =root
password =name
folderPath =C:/Users/name/Desktop/DataBase/

错误信息如下:

FAILURE: Build failed with an exception.

* Where:
 Build file 'C:\Users\name\IdeaProjects\HelloWorld\build.gradle' line: 36

* What went wrong:
A problem occurred evaluating root project 'AI'.
      > Cannot cast object 'jdbc:mysql://127.0.0.1:3306/name' with class 
    'java.lang.String' to class 'int'

   * Try:
       Run with --stacktrace option to get the stack trace. Run with --info 
      or --debug option to get more log output.

      BUILD FAILED

     Total time: 12.448 secs
     Cannot cast object 'jdbc:mysql://127.0.0.1:3306/name' with class 
    'java.lang.String' to class 'int'
   13:01:41: External task execution finished 'build'

第36行是哪一行? - Oliver Charlesworth
1个回答

6

JavaExec.args 是一个列表,因此 args[<anything>] 被解释为“类似数组访问”,因此必须是整数,但是你给了它一个字符串。

请替换为

args
 [project.property('url'),project.property('username'),
  project.property('password'),project.property('folderPath')]

使用以下其中一种方法之一:

args
 ([project.property('url'),project.property('username'),
  project.property('password'),project.property('folderPath')])

args project.property('url'), project.property('username'),
  project.property('password'), project.property('folderPath')

args project.url, project.username, project.password, project.folderPath

args url, username, password, folderPath

所有应该是等效的。

非常感谢您的回复。我已经解决了问题。实际上,有三种不同类型的参数:1. 参数列表args List;2. 字符串数组args String[];3. args(Objects.....args) JavaExec。所以我应该使用第三个,但我一直在使用第二个 :) - JustStartedProgramming

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接