15,912,400 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by anktrive (Top 8 by date)
anktrive
7-Oct-11 3:01am
View
Thanks Richard.... I was able to get it working using the above code...I think I also have been using 64 bit JDK. will try with 32 bit JDK.
anktrive
5-Oct-11 4:16am
View
The include directives that are missing in the above code
#include jni.h
#include windows.h
anktrive
5-Oct-11 4:15am
View
Deleted
The include directives that are missing in the above code
#include <jni.h>
#include <windows.h>
anktrive
5-Oct-11 4:14am
View
Here is the C code file I am trying to compile.
#include <jni.h>
#include <windows.h>
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */
main() {
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
// this should contain the path to the JVM DLL file
HINSTANCE hVM = LoadLibrary("C:\\Program Files\\Java\\jdk1.6.0_27\\jre6\\bin\\client\\jvm.dll");
if (hVM == NULL)
{
DWORD dwe = GetLastError();
return -1;
}
typedef jint
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);
fpCJV CreateJavaVM = (fpCJV)::GetProcAddress(hVM, "JNI_CreateJavaVM");
res = CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = (*env)->FindClass(env, "Prog");
if (cls == NULL) {
goto destroy;
}
mid = (*env)->GetStaticMethodID(env, cls, "main",
"([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == NULL) {
goto destroy;
}
stringClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
destroy:
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DestroyJavaVM(jvm);
}
Could you please check if it gives any compilation error to you?
anktrive
5-Oct-11 3:41am
View
The following line gives a compilation error as "syntax error before ':' token "
fpCJV CreateJavaVM = (fpCJV)::GetProcAddress(hVM, "JNI_CreateJavaVM");
Am I missing any import header file?
Sorry for the dumb question but i am new to C programming as well.
anktrive
5-Oct-11 1:26am
View
When you say linking with the correct .lib file, does it mean giving the library path for jvm.lib to C compiler or setting it as LD_LIBRARY_PATH in the environment variables?
anktrive
4-Oct-11 7:32am
View
I have already checked it since i found it as one of the possible reasons why JVM could not be created. JVM.dll is there. Is there a possibility that it is not accesible at runtime even if it is at the correct location?
anktrive
4-Oct-11 5:51am
View
Hi Richard, Thanks for the quick response.... I have used the exact same code given in the link and used Dev c++ to compile it, so it should follow the rules. In this code only I have received Linker error. I suspect it is not able to locate the java executables on my system.The environment variables on my system are:
PATH: C:\Program Files\Java\jdk1.6.0_27\bin;C:\Program Files\Java\jdk1.6.0_27\jre\bin\client
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_27\bin
LD_LIBRARY_PATH= C:\Program Files\Java\jdk1.6.0_27\jre\bin\client
CLASSPATH= C:\data
Could you please let me know if any of these system variables should be defined differently?
Show More