Skip to content

Commit aa4dae1

Browse files
Verify behavior of JVM.executeMain method
1 parent ebb49cb commit aa4dae1

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/java/os-environment/src/main/java/org/enso/os/environment/jni/JVM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static JVM create(String javaHome, String... options) {
4040
* @param classNameWithSlashes class (with `/` as separators) to search main method in
4141
* @param args arguments to pass to the main method
4242
*/
43-
public void executeMain(String classNameWithSlashes, String[] args) {
43+
public void executeMain(String classNameWithSlashes, String... args) {
4444
var e = env();
4545
try (var className = CTypeConversion.toCString(classNameWithSlashes);
4646
var mainName = CTypeConversion.toCString("main");

lib/java/os-environment/src/test/java/org/enso/os/environment/jni/LoadClassTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.enso.os.environment.jni;
22

3+
import java.io.File;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.net.URL;
7+
import java.nio.file.Files;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
310
import static org.junit.Assert.assertEquals;
411
import static org.junit.Assert.assertTrue;
512

@@ -10,11 +17,20 @@
1017

1118
public class LoadClassTest {
1219
private static final String PATH = System.getProperty("java.home");
20+
private static final URI JAR;
21+
static {
22+
try {
23+
JAR = LoadClassTest.class.getProtectionDomain().getCodeSource().getLocation().toURI();
24+
} catch (URISyntaxException ex) {
25+
throw new IllegalStateException(ex);
26+
}
27+
}
1328
private static JVM jvm;
1429

1530
private static JNI.JNIEnv env() {
1631
if (jvm == null) {
17-
jvm = JVM.create(PATH, "-Dsay=Ahoj");
32+
var cp = new File(JAR);
33+
jvm = JVM.create(PATH, "-Dsay=Ahoj", "-Djava.class.path=" + cp);
1834
}
1935
return jvm.env();
2036
}
@@ -87,4 +103,13 @@ public void setSystemProperty() {
87103
strReleaseFn.call(env, res, chars);
88104
}
89105
}
106+
107+
@Test
108+
public void executeMainClass() throws Exception {
109+
var out = File.createTempFile("check-main", ".log");
110+
jvm.executeMain("org/enso/os/environment/jni/TestMain", out.getPath());
111+
var content = Files.readString(out.toPath());
112+
assertEquals("Ciao", content);
113+
out.delete();
114+
}
90115
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.enso.os.environment.jni;
2+
3+
import java.io.File;
4+
import java.io.FileWriter;
5+
6+
public final class TestMain {
7+
private TestMain() {
8+
}
9+
10+
public static void main(String... args) throws Exception {
11+
var out = new File(args[0]);
12+
try (java.io.FileWriter os = new FileWriter(out)) {
13+
os.write("Ciao");
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)