Run an external jar file

Hi,

Groovy/Gradle newbie here :slight_smile:

Iā€™m working on a gradle script where one of the task needs to do the following:

-> Execute a pre-built jar file, which expects couple of arguments/parameters. The jar file depends on some other files from the same directory where the jar file is located.

At the moment, I donā€™t need to compile/build anything from a java source code. I already have a jar file that needs to be executed in a task along with few other tasks.

Iā€™m going through gradle documentation and searching online too, but most info seems to be revolving around compiling and building from java source code. I believe in my case, I just need to use the ā€˜applicationā€™ plugin but I donā€™t think Iā€™m doing it properly.

Hereā€™s what my build.gradle looks like right now:

apply plugin: 'application'
  task runScheduleReader << {
 javaexec {
  main = "ClassName"
  classpath = "lib/JarFileName"
  args('141028', 'abcd')
  }
}

The folder structure is pretty simple. at the root, I have build.gradle file, whose content is posted above. I have a directory named ā€œlibā€ where Iā€™ve stored the jar file and other files it requires. The 3rd line is where Iā€™m (trying to) pass parameter that is required by the jar file for execution.

Iā€™ve tried few other options, but not getting anywhere. Would someone be able to point me to a direction what exact Iā€™m missing here? Even if I have the source code to re-build it using Gradle (which I donā€™t think is necessary), will it compile the source code every time the build script is run, even though the source code for that java program is not changing.

1 Like

Iā€™m now able to run/execute the jar file. Looks like my classpath syntax wasnā€™t accurate.

I needed to use the following in classpath

classpath = files("lib/MyJarFile.jar")

Butā€¦ the jar file failes to access/open a property file that is located in the same directory as the jar file itself

Hi Aver,

Youā€™re close. Itā€™s OK to use javaexec {}, but usually itā€™s better to use the JavaExec task type. Itā€™s similar to Exec.

I donā€™t think you need any plug-ins for this unless youā€™re already building something else. I think youā€™re having trouble now because you need to set the working directory.

I would do something like this:

configurations {
    scheduleRuntime
 }
dependencies {
    scheduleRuntime files("lib/JarFileName.jar")
}
task runScheduleReader(type: JavaExec) {
    main = "ClassName"
    workingDir = "lib"
    classpath = configurations.scheduleRuntime
    args('141028', 'abcd')
  }

The configurations/dependencies blocks might seem extra, but Iā€™d only use an explicit file as a classpath if I was sure the jar would contain everything I needed to run (i.e., no other runtime dependencies). If your jar has any other dependencies, you can just add them to the scheduleRuntime configuration as a regular dependency (e.g., ā€˜log4j:log4j:1.2.17ā€™).

1 Like

Thank you Sterling for your reply. Youā€™re right that in this case I donā€™t need to use any plugin.

I do like your approach and Iā€™m trying to use it but keep getting an error during the build. Itā€™s complaining about the quotes in following line:

scheduleRuntime files("lib/JarFileName.jar")

The error Iā€™m getting is ā€œUnexpected tokenā€ and then it points to the first quote. I donā€™t see any typo in my code and based on Gradleā€™s documentation link below, it seems I have it correct. Not sure why Gradle is complaining about this line.

http://gradle.org/docs/current/userguide/dependency_management.html#sub:file_dependencies

The jar file Iā€™m working with does have other dependencies, which are all located in the same ā€œlibā€ directory where jar itself is located. Do I need to use filetree instead of files in the dependencies?

My guess is you have a typo somewhere else (mixing single quotes and double quotes).

You can use a filetree if you want to include a directory of jars as dependencies: http://stackoverflow.com/questions/11638555/gradle-dependencies-with-file-directories

Nope. I commented out everything in the script except for the configurations and dependencies block. It was still complaining about ā€œunexpected tokenā€ on that dependency. I changed it to use fileTree method as shown below and Iā€™m able to run the script now.

runtime fileTree(dir: 'lib', include: '*.*', exclude: 'output/*')

P.S. I changed the configuration name to runtime

Thank you for your help. Still curious why files method in that dependency was giving me trouble.

Smart quotes? Glad you got it working.