- if (!jhost) {
- jxbt_throw_jni(env, "global ref allocation failed");
- return NULL;
- }
- /* Sets the host name */
- const char *name = MSG_host_get_name(host);
- jobject jname = env->NewStringUTF(name);
- env->SetObjectField(jhost, jxbt_get_jfield(env,
- env->FindClass("org/simgrid/msg/Host"), "name", "Ljava/lang/String;"),
- jname);
- /* Bind & store it */
- jhost_bind(jhost, host, env);
- host->extension_set(JAVA_HOST_LEVEL, (void *) jhost);
- } else {
- jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
+JNIEXPORT jobject JNICALL Java_org_simgrid_msg_VM_getVMByName(JNIEnv* env, jclass cls, jstring jname)
+{
+ /* get the C string from the java string */
+ if (jname == nullptr) {
+ jxbt_throw_null(env, "No VM can have a null name");
+ return nullptr;
+ }
+ const char* name = env->GetStringUTFChars(jname, nullptr);
+ /* get the VM by name (VMs are just special hosts, unfortunately) */
+ auto const* host = sg_host_by_name(name);
+
+ if (not host) { /* invalid name */
+ jxbt_throw_host_not_found(env, name);
+ env->ReleaseStringUTFChars(jname, name);
+ return nullptr;