Skip to content

Commit 141df7d

Browse files
committed
Options passed to init are combined with those via addClassPath, addOpts
Address #59 Path added with `addClassPath` ignored by `init(opts)` method
1 parent 8537a1b commit 141df7d

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

src/jvm.jl

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,16 @@ addJarsToClassPath(args...) = isloaded() ?
232232
@warn("JVM already initialized. This call has no effect") :
233233
_addJarsToClassPath(args...)
234234

235-
addOpts(s::String) = isloaded() ? @warn("JVM already initialised. This call has no effect") : push!(opts, s)
236-
237-
function init()
238-
if isempty(cp)
239-
init(opts)
235+
function addOpts(s::String)
236+
if isloaded()
237+
@warn("JVM already initialised. This call has no effect")
240238
else
241-
ccp = collect(cp)
242-
options = collect(opts)
243-
classpath = "-Djava.class.path="*join(ccp,sep)
244-
init(vcat(options, classpath))
239+
m = match(r"^-Djava.class.path=(.*)",s)
240+
if m != nothing
241+
addClassPath(String(m.captures[1]))
242+
else
243+
push!(opts, s)
244+
end
245245
end
246246
end
247247

@@ -267,8 +267,46 @@ isloaded() = isdefined(JavaCall, :jnifunc) && isdefined(JavaCall, :penv) && penv
267267
assertloaded() = isloaded() ? nothing : throw(JavaCallError("JVM not initialised. Please run init()"))
268268
assertnotloaded() = isloaded() ? throw(JavaCallError("JVM already initialised")) : nothing
269269

270+
"""
271+
JavaCall.init(opts::Array{String,1})
272+
JavaCall.init(opt1::String,opt2::String, ...)
273+
JavaCall.init()
274+
275+
Initialize JavaCall with JVM options.
276+
277+
As of JavaCall v0.7.4 new options passed to init will be appended
278+
to previous options added with addClasspath and addOpts.
279+
280+
Once init() is called, addClasspath and addOpts no longer have any effect.
281+
282+
See http://juliainterop.github.io/JavaCall.jl/methods.html
283+
284+
Example
285+
JavaCall.init(["-Xmx512M", "-Djava.class.path=$(@__DIR__)", "-verbose:jni", "-verbose:gc"])
286+
"""
287+
function init(opts::Array{String,1})
288+
addOpts.(opts)
289+
init()
290+
end
291+
292+
# Accept options strings as a set of arguments
293+
init(opts::Array{AbstractString,1}) = init(String.(opts))
294+
init(opts::Vararg{AbstractString,N}) where N = init(String.([opts...]))
295+
296+
function init()
297+
if isempty(cp)
298+
_init(opts)
299+
else
300+
ccp = collect(cp)
301+
options = collect(opts)
302+
classpath = "-Djava.class.path="*join(ccp,sep)
303+
_init(vcat(options, classpath))
304+
end
305+
end
306+
307+
# Below is the original main initialization option
270308
# Pointer to pointer to pointer to pointer alert! Hurrah for unsafe load
271-
function init(opts)
309+
function _init(opts)
272310
assertnotloaded()
273311
assertroottask_or_goodenv()
274312
opt = [JavaVMOption(pointer(x), C_NULL) for x in opts]

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ JAVACALL_FORCE_ASYNC_INIT = get(ENV,"JAVACALL_FORCE_ASYNC_INIT","") ∈ ("1","ye
1616
JAVACALL_FORCE_ASYNC_TEST = get(ENV,"JAVACALL_FORCE_ASYNC_TEST","") ("1","yes")
1717

1818
@testset "initialization" begin
19+
JavaCall.addClassPath("foo")
20+
JavaCall.addOpts("-Djava.class.path=bar")
21+
JavaCall.addOpts("-Xmx512M")
1922
if JavaCall.JULIA_COPY_STACKS || JAVACALL_FORCE_ASYNC_INIT
2023
@testasync JavaCall.init(["-Djava.class.path=$(@__DIR__)"])==nothing
2124
else
2225
@test JavaCall.init(["-Djava.class.path=$(@__DIR__)"])==nothing
2326
end
27+
@test match(r"foo[:;]+bar",JavaCall.getClassPath()) != nothing
2428
# JavaCall.init(["-verbose:gc","-Djava.class.path=$(@__DIR__)"])
2529
# JavaCall.init()
2630
end

0 commit comments

Comments
 (0)