Click here to Skip to main content
15,887,027 members
Home / Discussions / Java
   

Java

 
GeneralRe: How to convert string to double with trailing zeros after decimal Pin
Member 161354338-Nov-23 2:23
Member 161354338-Nov-23 2:23 
GeneralRe: How to convert string to double with trailing zeros after decimal Pin
Richard MacCutchan8-Nov-23 2:35
mveRichard MacCutchan8-Nov-23 2:35 
GeneralRe: How to convert string to double with trailing zeros after decimal Pin
Dave Kreskowiak8-Nov-23 3:20
mveDave Kreskowiak8-Nov-23 3:20 
GeneralRe: How to convert string to double with trailing zeros after decimal Pin
jschell8-Nov-23 6:12
jschell8-Nov-23 6:12 
AnswerRe: How to convert string to double with trailing zeros after decimal Pin
Ralf Meier8-Nov-23 8:02
mveRalf Meier8-Nov-23 8:02 
AnswerRe: How to convert string to double with trailing zeros after decimal Pin
Dave Kreskowiak8-Nov-23 2:23
mveDave Kreskowiak8-Nov-23 2:23 
QuestionHow to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 0:54
Valentinor16-Oct-23 0:54 
AnswerRe: How to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 1:42
Valentinor16-Oct-23 1:42 
I managed to make it work:
C++
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include <jni.h>
#include <Windows.h>

JavaVM* jvm = nullptr;

std::string createVM(std::string location) {
	std::string jvmLocation = location;
	jvmLocation.append("ThirdParty\\Eclipse Adoptium\\jre-17.0.7.7-hotspot\\bin\\servers\\jvm.dll");
	HMODULE hJVMDLL = LoadLibraryA(jvmLocation.c_str());
	typedef jint(JNICALL* fpCJV)(JavaVM**, void**, void*);
	if (hJVMDLL != NULL) {
		fpCJV JNI_CreateJavaVM = (fpCJV)::GetProcAddress(hJVMDLL, "JNI_CreateJavaVM");
		location.insert(0, "-Djava.class.path=");
		location.append("Data\\Java");
		JavaVMInitArgs vm_args;
		JavaVMOption* options = new JavaVMOption[1];
		options[0].optionString = &location[0];
		vm_args.version = JNI_VERSION_10;
		vm_args.nOptions = 1;
		vm_args.options = options;
		vm_args.ignoreUnrecognized = false;
		JNIEnv* env = nullptr;
		jint rc = JNI_OK;
		if (jvm == nullptr) {
			rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
		}
		else {
			rc = jvm->AttachCurrentThread((void**)&env, NULL);
		}
		delete[] options;
		if (rc != JNI_OK) {
			if (rc == JNI_EVERSION)
				return "JNI_EVERSION";
			else if (rc == JNI_ENOMEM)
				return "JNI_ENOMEM";
			else if (rc == JNI_EINVAL)
				return "JNI_EINVAL";
			else if (rc == JNI_EEXIST)
				return "JNI_EEXIST";
			else
				return "JNI_FATALERROR";
		}
		return "JNI_CREATED";
	}
	else {
		return "ERROR_LOADING_DLL";
	}
}

std::string createIdentification() {
	JNIEnv* env;
	jvm->AttachCurrentThread((void**)&env, NULL);
	jclass jClass = env->FindClass("JavaMethods");

	if (jClass == nullptr) {
		return "ClassNotFound cI";
	}
	else {
		jmethodID methodID = env->GetStaticMethodID(jClass, "createIdentification", "()Ljava/lang/String;");
		if (methodID == nullptr) {
			return "MethodNotFound cI";
		}
		else {
			jboolean isCopy;
			jstring jResult = (jstring)env->CallStaticObjectMethod(jClass, methodID);
			const char* string = env->GetStringUTFChars(jResult, &isCopy);
			std::string result = string;
			env->ReleaseStringUTFChars(jResult, string);

			return result;
		}
	}
}

int main() {
	std::cout << createVM("Location_To_App_Folder").c_str() << std::endl;
	if (jvm != NULL) {
		std::cout << createIdentification().c_str();
	}

	std::this_thread::sleep_for(std::chrono::milliseconds(2000));
	return 0;
}


Thank you @Richard-MacCutchan for the original code!
AnswerRe: How to use JNI without setting Environment Variables Pin
jschell16-Oct-23 4:58
jschell16-Oct-23 4:58 
GeneralRe: How to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 5:41
Valentinor16-Oct-23 5:41 
GeneralRe: How to use JNI without setting Environment Variables Pin
jschell17-Oct-23 5:47
jschell17-Oct-23 5:47 
GeneralRe: How to use JNI without setting Environment Variables Pin
Valentinor17-Oct-23 19:26
Valentinor17-Oct-23 19:26 
QuestionSending file though socket as byte[] Pin
JohnCodding25-Sep-23 20:52
JohnCodding25-Sep-23 20:52 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding25-Sep-23 21:33
JohnCodding25-Sep-23 21:33 
GeneralRe: Sending file though socket as byte[] Pin
Richard MacCutchan25-Sep-23 22:32
mveRichard MacCutchan25-Sep-23 22:32 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding25-Sep-23 23:44
JohnCodding25-Sep-23 23:44 
GeneralRe: Sending file though socket as byte[] Pin
Richard MacCutchan25-Sep-23 23:58
mveRichard MacCutchan25-Sep-23 23:58 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding26-Sep-23 0:53
JohnCodding26-Sep-23 0:53 
GeneralRe: Sending file though socket as byte[] Pin
jschell3-Oct-23 4:42
jschell3-Oct-23 4:42 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding12-Oct-23 23:35
JohnCodding12-Oct-23 23:35 
GeneralRe: Sending file though socket as byte[] Pin
jschell13-Oct-23 7:19
jschell13-Oct-23 7:19 
Question[Solved] Getting and setting a File's all attributes Pin
Valentinor24-Sep-23 22:47
Valentinor24-Sep-23 22:47 
AnswerRe: Getting and setting a File's all attributes Pin
Victor Nijegorodov24-Sep-23 23:04
Victor Nijegorodov24-Sep-23 23:04 
GeneralRe: Getting and setting a File's all attributes Pin
Valentinor24-Sep-23 23:17
Valentinor24-Sep-23 23:17 
Answer[Solution] Re: Getting and setting a File's all attributes Pin
Valentinor25-Sep-23 6:26
Valentinor25-Sep-23 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.