Coder Social home page Coder Social logo

jythonscript's Introduction

JythonScript 2.0.x

Build Status Coverage Status Maven Central

Description:

JythonScript is a simple Jython wrapper for easier execution and/or evaluation of Python expressions and scripts at runtime for JVM-based languages.

Maven

<dependency>
  <groupId>com.github.adchilds</groupId>
  <artifactId>jythonscript</artifactId>
  <version>2.0.1</version>
</dependency>

Dependencies:

None. JythonScript packages the Jython standalone JAR as part of it's distribution, so no further dependencies are required.

Examples:

test.py:

from java.lang import System

def test():
	System.out.println('Hello from Jython')

if __name__ == '__main__':
	test()

Test.java:

import com.github.adchilds.jython.JythonScript;

public class Test {

	public static void main(String[] args) {
		String filePath = ClassLoader.getSystemResource("test.py").getPath();
		
		JythonScript.execute(filePath);
	}
}

Output: "Hello from Jython"

test2.py:

import sys

def multiply(a, b):
	return a * b

if __name__ == '__main__':
	a = sys.argv[1]
	b = sys.argv[2]

	result = multiply(a, b)

Test2.java

import com.github.adchilds.jython.JythonScript;

public class Test2 {

	public static void main(String[] args) {
		String filePath = ClassLoader.getSystemResource("test2.py").getPath();
		
		Integer result = JythonScript.evaluate(filePath, 5, 5);
		System.out.println("Result = [" + result + "]");
	}
}

Output: "Result = [25]"

Test3.java

import com.github.adchilds.jython.JythonScript;
	
public class Test2 {
	
	public static void main(String[] args) {
		String script = "from java.lang import System\n" +
                        "\t\n" +
                        "\tdef test():\n" +
                        "\t\tSystem.out.println('Hello from Jython')\n" +
                        "\t\n" +
                        "\tif __name__ == '__main__':\n" +
                        "\t\ttest()";
		
		PyCode compiledScript = JythonScript.compileString(script);
		JythonScript.execute(compiledScript);
	}
}

Output: "Hello from Jython"

Useful Links:

jythonscript's People

Contributors

adchilds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jasamuels

jythonscript's Issues

Can't load the python script paths?

When I used //String filePath = ClassLoader.getSystemResource("test2.py").getPath();// and created a python file named test2.py in the same directory, program shows a nullpointer exception in this line! what to do?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.