jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 181 forks source link

Unable to start JVM through JPype on Amazon linux #948

Closed jain18aditya closed 3 years ago

jain18aditya commented 3 years ago

I am using JPype to call Java code from my python tests and starting JVM via Jpype.

I am using custom classpath and default JVM path for execution. In the error it is able to find the classpath and default jvm path.

This is giving error while starting the JVM

jvmpath = '/usr/lib/jvm/java-1.8.0-amazon-corretto/jre/lib/amd64/server/libjvm.so', classpath = ['/home/jaiadi/test_new/*'], ignoreUnrecognized = False
convertStrings = False

    def startJVM(*args, **kwargs):
        """
        Starts a Java Virtual Machine.  Without options it will start
        the JVM with the default classpath and jvmpath.

        The default classpath is determined by ``jpype.getClassPath()``.
        The default jvmpath is determined by ``jpype.getDefaultJVMPath()``.

        Parameters:
         *args (Optional, str[]): Arguments to give to the JVM.
            The first argument may be the path the JVM.

        Keyword Arguments:
          jvmpath (str):  Path to the jvm library file,
            Typically one of (``libjvm.so``, ``jvm.dll``, ...)
            Using None will apply the default jvmpath.
          classpath (str,[str]): Set the classpath for the JVM.
            This will override any classpath supplied in the arguments
            list. A value of None will give no classpath to JVM.
          ignoreUnrecognized (bool): Option to ignore
            invalid JVM arguments. Default is False.
          convertStrings (bool): Option to force Java strings to
            cast to Python strings. This option is to support legacy code
            for which conversion of Python strings was the default. This
            will globally change the behavior of all calls using
            strings, and a value of True is NOT recommended for newly
            developed code.

            The default value for this option during 0.7 series is
            True.  The option will be False starting in 0.8. A
            warning will be issued if this option is not specified
            during the transition period.

        Raises:
          OSError: if the JVM cannot be started or is already running.
          TypeError: if an invalid keyword argument is supplied
            or a keyword argument conflicts with the arguments.

         """
        if _jpype.isStarted():
            raise OSError('JVM is already started')
        global _JVM_started
        if _JVM_started:
            raise OSError('JVM cannot be restarted')

        args = list(args)

        # JVM path
        jvmpath = None
        if args:
            # jvm is the first argument the first argument is a path or None
            if not args[0] or not args[0].startswith('-'):
                jvmpath = args.pop(0)
        if 'jvmpath' in kwargs:
            if jvmpath:
                raise TypeError('jvmpath specified twice')
            jvmpath = kwargs.pop('jvmpath')
        if not jvmpath:
            jvmpath = getDefaultJVMPath()

        # Classpath handling
        if _hasClassPath(args):
            # Old style, specified in the arguments
            if 'classpath' in kwargs:
                # Cannot apply both styles, conflict
                raise TypeError('classpath specified twice')
            classpath = None
        elif 'classpath' in kwargs:
            # New style, as a keywork
            classpath = kwargs.pop('classpath')
        else:
            # Not speficied at all, use the default classpath
            classpath = _classpath.getClassPath()

        # Handle strings and list of strings.
        if classpath:
            if isinstance(classpath, str):
                args.append('-Djava.class.path=%s' % _handleClassPath([classpath]))
            elif hasattr(classpath, '__iter__'):
                args.append('-Djava.class.path=%s' % _handleClassPath(classpath))
            else:
                raise TypeError("Unknown class path element")

        ignoreUnrecognized = kwargs.pop('ignoreUnrecognized', False)
        convertStrings = kwargs.pop('convertStrings', False)

        if kwargs:
            raise TypeError("startJVM() got an unexpected keyword argument '%s'"
                            % (','.join([str(i) for i in kwargs])))

        try:
            _jpype.startup(jvmpath, tuple(args),
>                          ignoreUnrecognized, convertStrings)
E                          TypeError: function takes exactly 5 arguments (4 given)
jpype/_core.py:212: TypeError
___

My code looks like this for starting JVM

            JAR_CLASSPATH = ['/home/jaiadi/test_new/*']
            jpype.startJVM(jpype.getDefaultJVMPath(), classpath=JAR_CLASSPATH, convertStrings=False)

Any idea on why i am getting param error?

Thrameos commented 3 years ago

I don't see anything unusual that should be causing an error.

jain18aditya commented 3 years ago

Not sure whats the reason but downgrading to 1.2.1 solved this issue. I hope its not bug with the latest.

Thrameos commented 3 years ago

I verified there have been no code changes to jpype since 1.2.1. So most likely this was a local site build problem of some kind.