Skip to content
Permalink
Browse files

Merge pull request #444 from Thrameos/start_jvm

Final work on startJVM
  • Loading branch information...
Thrameos committed Jun 1, 2019
2 parents 6b85ead + 5b1473a commit 5b359f19b74fb2d65b34900cecbb41ac7209a55c
@@ -63,3 +63,5 @@ target/
*.swp
*.stackdump
tmp
doc/html
native/jars
@@ -95,48 +95,85 @@ def _hasClassPath(args):

_JVM_started = False

def startJVM(jvm=None, *args, **kwargs):
def startJVM(*args, **kwargs):
"""
Starts a Java Virtual Machine. Without options it will start
the JVM with the default classpath and jvm. The default classpath
will be determined by ``jpype.getClassPath()``. The default JVM is
determined by ``jpype.getDefaultJVMPath()``.
the JVM with the default classpath and jvmpath.
Args:
jvm (Optional, str): Path to the jvm library file,
The default of None will use ``jpype.getDefaultJVMPath()``
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``, ...)
*args (Optional, str[]): Arguments to give to the JVM
classpath (Optional[string]): Set the classpath for the jvm.
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. Default will use ``jpype.getClassPath``
ignoreUnrecognized (Optional, [bool]): Option to JVM to ignore
invalid JVM arguments. Default is False.
"""
list. A value of None will give no classpath to JVM.
ignoreUnrecognized (bool): Option to JVM to ignore
invalid JVM arguments. Default is False.
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')
_JVM_started = True

if jvm is None:
jvm = getDefaultJVMPath()

# Check to see that the user has not set the classpath
# Otherwise use the default if not specified
if not _hasClassPath(args) and 'classpath' not in kwargs:
kwargs['classpath'] = _classpath.getClassPath()
args = list(args)

if 'ignoreUnrecognized' not in kwargs:
kwargs['ignoreUnrecognized'] = False
# 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
args = list(args)
if 'classpath' in kwargs and kwargs['classpath'] != None:
args.append('-Djava.class.path=%s' % (kwargs['classpath']))
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, _jtypes._unicode)):
args.append('-Djava.class.path=%s' % classpath)
else:
args.append('-Djava.class.path=%s' %
(_classpath._SEP.join(classpath)))

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

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

_jpype.startup(jvm, tuple(args), kwargs['ignoreUnrecognized'])
_jpype.startup(jvmpath, tuple(args), ignoreUnrecognized)
_initialize()


@@ -65,7 +65,9 @@ namespace JPError
_value_error = 4,
_overflow_error = 5,
_index_error = 6,
_attribute_error = 7
_attribute_error = 7,
_os_error_unix = 8,
_os_error_windows = 9
} ;
}

@@ -79,6 +81,8 @@ namespace JPError
// have a lot for facitilies to make this easy.
#define JP_RAISE_PYTHON(msg) { throw JPypeException(JPError::_python_error, msg, JP_STACKINFO()); }
#define JP_RAISE_RUNTIME_ERROR(msg) { throw JPypeException(JPError::_runtime_error, msg, JP_STACKINFO()); }
#define JP_RAISE_OS_ERROR_UNIX(err, msg) { throw JPypeException(JPError::_os_error_unix, err, msg, JP_STACKINFO()); }
#define JP_RAISE_OS_ERROR_WINDOWS(err, msg) { throw JPypeException(JPError::_os_error_windows, err, msg, JP_STACKINFO()); }
#define JP_RAISE_TYPE_ERROR(msg) { throw JPypeException(JPError::_type_error, msg, JP_STACKINFO()); }
#define JP_RAISE_VALUE_ERROR(msg) { throw JPypeException(JPError::_value_error, msg, JP_STACKINFO()); }
#define JP_RAISE_OVERFLOW_ERROR(msg) { throw JPypeException(JPError::_overflow_error, msg, JP_STACKINFO()); }
@@ -141,6 +145,7 @@ class JPypeException
JPypeException(jthrowable, const char* msn, const JPStackInfo& stackInfo);
JPypeException(JPError::Type errorType, const char* msn, const JPStackInfo& stackInfo);
JPypeException(JPError::Type errorType, const string& msn, const JPStackInfo& stackInfo);
JPypeException(JPError::Type errorType, int err, const string& msn, const JPStackInfo& stackInfo);
JPypeException(const JPypeException& ex);

~JPypeException();
@@ -171,6 +176,7 @@ class JPypeException
JPStackTrace m_Trace;
string m_Message;
JPThrowableRef m_Throwable;
int m_Error;
} ;

/**
@@ -23,6 +23,7 @@
#else
#include <dlfcn.h>
#endif // HPUX
#include <errno.h>

class LinuxPlatformAdapter : public JPPlatformAdapter
{
@@ -41,9 +42,7 @@ class LinuxPlatformAdapter : public JPPlatformAdapter

if (jvmLibrary == NULL)
{
std::stringstream msg;
msg << "Unable to load DLL [" << path << "], error = " << dlerror();
JP_RAISE_RUNTIME_ERROR( msg.str().c_str());
JP_RAISE_OS_ERROR_UNIX( errno, path);
}
}

@@ -54,9 +54,7 @@ class Win32PlatformAdapter : public JPPlatformAdapter
jvmLibrary = LoadLibrary(path);
if (jvmLibrary == NULL)
{
std::stringstream msg;
msg << "Unable to load DLL [" << path << "], error = " << formatMessage(GetLastError());
JP_RAISE_RUNTIME_ERROR( msg.str());
JP_RAISE_OS_ERROR_WINDOWS( GetLastError(), path);
}
}

@@ -39,6 +39,8 @@ void JPClassLoader::init()

// Set up class loader
jmethodID ctorID = frame.GetMethodID(cls, "<init>", "(Ljava/lang/ClassLoader;)V");
if (ctorID == NULL)
JP_RAISE_RUNTIME_ERROR("JPypeClassLoader ctor not found");
//v.l = cl;

jmethodID getInstanceID = frame.GetStaticMethodID(cls, "getInstance", "()Lorg/jpype/classloader/JPypeClassLoader;");
@@ -44,6 +44,15 @@ JPypeException::JPypeException(JPError::Type errType, const string& msn, const J
from(stackInfo);
}

JPypeException::JPypeException(JPError::Type errType, int err, const string& msn, const JPStackInfo& stackInfo)
{
JP_TRACE("EXCEPTION THROWN", errType, msn);
m_Type = errType;
m_Message = msn;
m_Error = err;
from(stackInfo);
}

JPypeException::JPypeException(const JPypeException& ex)
: m_Trace(ex.m_Trace), m_Throwable(ex.m_Throwable)
{
@@ -285,6 +294,38 @@ void JPypeException::toPython()
PyErr_SetString(PyExc_AttributeError, mesg.c_str());
return;

case JPError::_os_error_unix:
{
PyObject* val = Py_BuildValue("(iz)", m_Error, (string("JVM DLL not found: ") + mesg).c_str());
if (val != NULL)
{
PyObject* exc = PyObject_Call(PyExc_OSError, val, NULL);
Py_DECREF(val);
if (exc != NULL)
{
PyErr_SetObject(PyExc_OSError, exc);
Py_DECREF(exc);
}
}
return;
}

case JPError::_os_error_windows:
{
PyObject* val = Py_BuildValue("(izzi)", 2, (string("JVM DLL not found: ") + mesg).c_str(), NULL, m_Error);
if (val != NULL)
{
PyObject* exc = PyObject_Call(PyExc_OSError, val, NULL);
Py_DECREF(val);
if (exc != NULL)
{
PyErr_SetObject(PyExc_OSError, exc);
Py_DECREF(exc);
}
}
return;
}

default:
JP_TRACE("Unknown Error");
PyErr_SetString(PyExc_RuntimeError, mesg.c_str());
@@ -55,6 +55,8 @@ void JPReferenceQueue::init()
//Required due to bug in jvm
//See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6493522
jmethodID ctor = frame.GetMethodID(cls, "<init>", "()V");
if (ctor == NULL)
JP_RAISE_RUNTIME_ERROR("JPypeReferenceQueue ctor not found");

JNINativeMethod method2[1];
method2[0].name = (char*) "removeHostReference";

0 comments on commit 5b359f1

Please sign in to comment.
You can’t perform that action at this time.