How to get Jython working

Is the plugin downloading the JAr’s for Jython?

thufir@doge:~/NetBeansProjects/nfl$ 
thufir@doge:~/NetBeansProjects/nfl$ gradle clean run
:clean UP-TO-DATE
:compileJava
Download https://plugins.gradle.org/m2/gradle/plugin/com/github/rzabini/gradle-jython/1.0.2/gradle-jython-1.0.2.pom
Download https://plugins.gradle.org/m2/de/undercouch/gradle-download-task/2.0.0/gradle-download-task-2.0.0.pom
Download https://plugins.gradle.org/m2/gradle/plugin/com/github/rzabini/gradle-jython/1.0.2/gradle-jython-1.0.2.jar
Download https://plugins.gradle.org/m2/de/undercouch/gradle-download-task/2.0.0/gradle-download-task-2.0.0.jar
/home/thufir/NetBeansProjects/nfl/src/main/java/net/bounceme/dur/nfl/JythonBean.java:15: error: package org.python.util does not exist
        org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
                       ^
/home/thufir/NetBeansProjects/nfl/src/main/java/net/bounceme/dur/nfl/JythonBean.java:15: error: package org.python.util does not exist
        org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
                                                                      ^
2 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* 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: 44.147 secs
thufir@doge:~/NetBeansProjects/nfl$ 

build file:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'

String mavenGroupId = 'thufir'
String mavenVersion = '1.0-SNAPSHOT'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'


// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.


mainClassName = 'net.bounceme.dur.nfl.Run'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'net.bounceme.dur.nfl.Run'
}

repositories {
 //   mavenCentral()
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
maven{
url "https://plugins.gradle.org/m2/"}
}

dependencies {
    //TODO: Add dependencies here ...
    //You can read more about how to add dependency here:
    //http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
    //testCompile group: 'junit', name: 'junit', version: '4.10'
//    compile 'com.google.api-client:google-api-client:1.20.0'
//	classpath "gradle.plugin.com.github.rzabini:gradle-jython:1.0.2"
	compile "gradle.plugin.com.github.rzabini:gradle-jython:1.0.2"
}

The plugin will download the jython standalone jar and create a dependency configuration for any additional python package dependencies you might have.

However, your buildscript is currently only downloading the maven dependencies of the plugin. It is being included as a Java dependency, and your build is not actually using this plugin. You need move your plugin repository / dependency to the buildscript block or use the newer plugins syntax. It looks like you tried to paste the plugin dependency into an existing template and match things up, but the template was likely missing the pieces you actually would need.

It should look like:

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "gradle.plugin.com.github.rzabini:gradle-jython:1.0.0"
    }
}

apply plugin: "com.github.rzabini.gradle-jython"

- OR -

plugins {
    id "com.github.rzabini.gradle-jython" version "1.0.2"
}
1 Like

Ok, that makes more sense:

thufir@doge:~/NetBeansProjects/nfl$ 
thufir@doge:~/NetBeansProjects/nfl$ gradle clean run
Download https://plugins.gradle.org/m2/gradle/plugin/com/github/rzabini/gradle-jython/1.0.0/gradle-jython-1.0.0.pom
Download https://plugins.gradle.org/m2/gradle/plugin/com/github/rzabini/gradle-jython/1.0.2/gradle-jython-1.0.2.pom
Download https://plugins.gradle.org/m2/de/undercouch/gradle-download-task/2.0.0/gradle-download-task-2.0.0.pom
Download https://plugins.gradle.org/m2/gradle/plugin/com/github/rzabini/gradle-jython/1.0.2/gradle-jython-1.0.2.jar
Download https://plugins.gradle.org/m2/de/undercouch/gradle-download-task/2.0.0/gradle-download-task-2.0.0.jar
:clean
:compileJava
:jythonClasses
:processResources UP-TO-DATE
:classes
:runJan 16, 2017 1:28:05 PM net.bounceme.dur.nfl.Run main
INFO: hello nfl
Jan 16, 2017 1:28:05 PM net.bounceme.dur.nfl.Run init
INFO: will now invoke jython
Jan 16, 2017 1:28:05 PM net.bounceme.dur.nfl.JythonBean <init>
INFO: java bean for now..
Jan 16, 2017 1:28:05 PM net.bounceme.dur.nfl.JythonBean createInterpreter
INFO: jython to follow


BUILD SUCCESSFUL

Total time: 6.876 secs
thufir@doge:~/NetBeansProjects/nfl$ 
thufir@doge:~/NetBeansProjects/nfl$ cat build.gradle

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }
    
    dependencies {
        classpath "gradle.plugin.com.github.rzabini:gradle-jython:1.0.0"
    }    
}

plugins {
    id "com.github.rzabini.gradle-jython" version "1.0.2"
    id "java"
    id "maven"
    id "application"
}


mainClassName = 'net.bounceme.dur.nfl.Run'


thufir@doge:~/NetBeansProjects/nfl$ 
thufir@doge:~/NetBeansProjects/nfl$ nano src/main/java/net/bounceme/dur/nfl/JythonBean.java 
thufir@doge:~/NetBeansProjects/nfl$ 
thufir@doge:~/NetBeansProjects/nfl$ gradle clean run
:clean
:compileJava/home/thufir/NetBeansProjects/nfl/src/main/java/net/bounceme/dur/nfl/JythonBean.java:15: error: package org.python.util does not exist
        org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
                       ^
/home/thufir/NetBeansProjects/nfl/src/main/java/net/bounceme/dur/nfl/JythonBean.java:15: error: package org.python.util does not exist
        org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
                                                                      ^
2 errors
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* 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: 2.824 secs
thufir@doge:~/NetBeansProjects/nfl$ 

How do I know or see that build is actually using the plugin? The new syntax is much more compact. If I were to print out the classpath from Gradle, I doubt it would actually show these JAR’s. It’s still not using Jython properly, but have a better start.

It is possible to see the build dependencies and the path to the JARs that you are including in the classpath.

To show the dependencies of the build itself, run the command:
gradle buildEnvironment

If you want see the paths to each of the JARs, you can add a task that does this in your buildscript:

task printBuildscriptClasspath {
    doLast { println project.buildscript.configurations.classpath.asPath }
}

You can likely skip paying particular attention to the buildscript classpath though. Applying the plugin should cause a noticeable difference in the build behavior, or minimally in the build properties. In this case, running gradle properties should show show additional configurations called jython and pythonpath, an jython extension, and a jythonClassses task.

You didn’t go into much detail about what you’re trying to do with jython. The plugin you’re using is intended for writing Gradle tasks that execute python scripts with jython. However, the error looks like you might be trying to embed jython in a Java application, which isn’t exactly the same use case.

Any chance I might re-open this thread? The title is the one I would have given to a new thread…

I’m using Gradle 4.4.1 and Eclipse Oxygen and I found that Jython-Gradle plugin (rzabini) which raised great hopes… only to have them dashed when I realised, as you (James) point out, it is not intended for someone who is keen on using Jython per se. In particular, I haven’t found out how you import outside Java .jars… and in an Issue I recently opened the author didn’t know either.

I don’t want to embed Jython (best language in the world TM) in Java, I just want to use Jython basically instead of Java, including JUnit testing, etc. … using Gradle as my build tool of choice.

Any idea whether this is possible?

If you don’t want to embed Jython in Java, I don’t think I understand what else you want the plugin to do for you. Using the task type provided by the plugin is equivalent to running jython with your entry point and the appropriate classpath for you Java dependencies. The plugin wasn’t designed to generate a JAR that contains class files generated by jythonc, but that sounds like what you don’t want.

Can you explain exactly what you expect to be your inputs and outputs for the tasks in the “build” process?