Skip to content

Commit 8fa7433

Browse files
committed
[[ JavaFFI ]] Use env var to set classpath of jvm instance
This patch initialises the jvm on desktop platforms using the value of environment variable CLASSPATH, thereby allowing classes in a jar file to be used with LCB. The CLASSPATH must be set before the first use of Java FFI
1 parent 66c5e9b commit 8fa7433

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Java CLASSPATH support
2+
3+
Limited support is available for loading custom Java classes
4+
and .jar files in the IDE on Mac and Linux. If the CLASSPATH
5+
environment variable is set before the Java virtual machine
6+
is initialised (i.e. before Java FFI is used), then any paths
7+
specified are added to the locations searched by the default
8+
class loader.
9+

libfoundation/src/foundation-java-private.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,25 @@ bool initialise_jvm()
330330
vm_args.version = JNI_VERSION_1_6;
331331
init_jvm_args(&vm_args);
332332

333+
const char *t_class_path = getenv("CLASSPATH");
334+
if (t_class_path == nullptr)
335+
{
336+
t_class_path = "/usr/lib/java";
337+
}
338+
339+
char *t_option = strdup("-Djava.class.path=");
340+
t_option = strcat(t_option, t_class_path);
341+
333342
JavaVMOption* options = new (nothrow) JavaVMOption[1];
334-
options[0].optionString = const_cast<char*>("-Djava.class.path=/usr/lib/java");
343+
options[0].optionString = t_option;
335344

336345
vm_args.nOptions = 1;
337346
vm_args.options = options;
338347
vm_args.ignoreUnrecognized = false;
339348

340-
return create_jvm(&vm_args);
349+
bool t_success = create_jvm(&vm_args);
350+
free(t_option);
351+
return t_success;
341352
#endif
342353
return true;
343354
}

0 commit comments

Comments
 (0)