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.

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

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.