Comments (13)
For example with the help of JavaCPP.
https://github.com/hoary/JavaAV
I can use this library from C++ directly.
from javacpp.
It wasn't designed for that, no... Check the README.md file for some options.
from javacpp.
I know but its not that hard to do with ANTLR Library
from javacpp.
I'm not sure why ANTLR would help with that. We don't really need to parse anything... Am I missing something?
from javacpp.
so how can you convert Java code to C++ ?? I can help you about library.
I have some source codes for you
from javacpp.
You can call JavaVM inside C++ with the help of JNI Library on C++
from javacpp.
You can invoke with that code:
JavaVM *InitJVM(JNIEnv **env2) {
JavaVM* vm;
JNIEnv* env;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
CHAR lpDataTempClass[MAX_PATH];
CHAR lpchDataLib[MAX_PATH];
TCHAR lpchRuntimelib[MAX_PATH];
TCHAR lpchJavaPath[MAX_PATH];
jobjectArray joApplicationArgs;
*env2 = NULL;
/* disable JIT */
//options[0].optionString ="-Djava.compiler=NONE";
/* user classes */
//"-Djava.class.path=c:\\myclasses"
//Pointer to the function JNI_CreateJavaVM
HKEY key;
BOOL foundDLL;
TCHAR lpchRuntimePath[MAX_PATH];
TCHAR lpchJavaHome[MAX_PATH];
TCHAR lpchThisPath[MAX_PATH];
GetThisPath(lpchThisPath,MAX_PATH);
HRESULT result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\JavaSoft\\Java Runtime Environment", 0, KEY_QUERY_VALUE,&key);
char *lpVal = readStringRegValue(key, "CurrentVersion");
if ( lpVal != NULL ) {
TCHAR lpchJVMRuntime[MAX_PATH];
HKEY subkey;
result = RegOpenKeyExA(key, lpVal, 0, KEY_QUERY_VALUE, &subkey);
if(result == ERROR_SUCCESS) {
readStringRegValueW(subkey, _T("RuntimeLib"), lpchJVMRuntime );
readStringRegValueW(subkey, L"JavaHome", lpchJavaHome);
//lpchJavaHome = readStringRegValue( subkey , "JavaHome" );
if(hinst_JVM == NULL) {
//WCHAR achRuntimePath[MAX_PATH];
//MultiByteToWideChar( CP_ACP , 0 , lpchJVMRuntime , -1 , achRuntimePath , MAX_PATH );
//CHAR *achCustomLib = "C:\\Program Files\\Java\\jre6\\bin\\client\\jvm.dll";
hinst_JVM = LoadLibraryW( lpchJVMRuntime );
DWORD errCode = GetLastError( );
if (!hinst_JVM) {
WCHAR lpchJVMBinPath[MAX_PATH];
WCHAR lpchFullFilePath[MAX_PATH];
WCHAR lpchJavaHomeW[MAX_PATH];
WCHAR lpchClientPath[MAX_PATH];
//hinst_JVM = LoadLibraryA("jvm.dll");
EnumerateSubKeys(HKEY_LOCAL_MACHINE, "Software\\JavaSoft\\Java Runtime Environment", &foundDLL, lpchRuntimePath, lpchJavaHome);
if (foundDLL) {
wsprintfW(lpchJVMBinPath, L"%s\\bin", lpchJavaHome);
wsprintfW(lpchClientPath, L"%s\\bin\\client\\", lpchJavaHome);
CVector<CUIString> values;
listFiles(lpchJVMBinPath, values);
for (int j = 0; j < values.size(); j++) {
CUIString *str = values.getElementAt(j);
WCHAR *value = str->c_str();
if (wcsstr(value, L"msvcr") && wcsstr(value, L".dll")) {
//lpchFullFilePath
WCHAR wchCombinedPath[MAX_PATH];
wsprintfW(wchCombinedPath, L"%s\\%s", lpchJVMBinPath, value);
wcscat(lpchClientPath, value);
CopyFile(wchCombinedPath, lpchClientPath, FALSE);
wcscat(lpchThisPath, _T("\\"));
wcscat(lpchThisPath, value);
CopyFile(wchCombinedPath, lpchThisPath, FALSE);
break;
}
}
}
else {
//no jvm
RegCloseKey(key);
RegCloseKey(subkey);
return NULL;
}
}
//jvm.dll found
else {
WCHAR lpchJVMBinPath[MAX_PATH];
WCHAR lpchFullFilePath[MAX_PATH];
WCHAR lpchJavaHomeW[MAX_PATH];
WCHAR lpchClientPath[MAX_PATH];
wsprintfW(lpchJVMBinPath, L"%s\\bin", lpchJavaHome);
wsprintfW(lpchClientPath, L"%s\\bin\\client\\", lpchJavaHome);
CVector<CUIString> values;
listFiles(lpchJVMBinPath, values);
for (int j = 0; j < values.size(); j++) {
CUIString *str = values.getElementAt(j);
WCHAR *value = str->c_str();
if (wcsstr(value, L"msvcr") && wcsstr(value, L".dll")) {
//lpchFullFilePath
WCHAR wchCombinedPath[MAX_PATH];
wsprintfW(wchCombinedPath, L"%s\\%s", lpchJVMBinPath, value);
wcscat(lpchClientPath, value);
CopyFile(wchCombinedPath, lpchClientPath, FALSE);
wcscat(lpchThisPath, _T("\\"));
wcscat(lpchThisPath, value);
CopyFile(wchCombinedPath, lpchThisPath , FALSE);
break;
}
}
}
}
RegCloseKey(subkey);
}
} else {
//NO JVM Installed
}
RegCloseKey(key);
if(hinst_JVM == NULL) {
return NULL;
}
//java -Djava.library.path=C:\Users\Kadir\IdeaProjects\WebcamTransmitter\nativeLib\windows\x64 -cp logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar service.WebcamCapture
//java -Djava.library.path=C:\Users\Kadir\IdeaProjects\WebcamTransmitter\nativeLib\windows\x86 -cp logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar service.WebcamCapture
//Get the address of the function
pfnCreateJavaVM = (CreateJavaVM_t*)GetProcAddress(hinst_JVM, "JNI_CreateJavaVM");
pfnGetDefaultJavaVMOptions = (OptionsJavaVM_t*)GetProcAddress(hinst_JVM, "JNI_GetDefaultJavaVMInitArgs");
//-Djava.class.path
//"logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar"
CHAR achTemp[MAX_PATH];
{
TCHAR achTemp2[MAX_PATH];
GetTempPath(MAX_PATH, achTemp2);
int sizeNeed = WideCharToMultiByte(CP_UTF8, 0, achTemp2, MAX_PATH, achTemp, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, achTemp2, MAX_PATH, achTemp, sizeNeed, NULL, NULL);
int dataLen = wcslen(achTemp2) - 1;
if (achTemp[dataLen] == '\\') {
achTemp[dataLen] = L'\0';
}
}
//SHCreateDirectoryEx(T("C:\\Foo\\Bar\\Baz"));
sprintf( lpDataTempClass, "-Djava.class.path=%s;%ls\\lib;", achTemp , lpchJavaHome );
options[ 0 ].optionString = lpDataTempClass;
CHAR lpDataTempClass2[ 2048 ];
CHAR lpData[MAX_PATH];
TCHAR lpData2[MAX_PATH];
GetThisPath(lpData2, 2048);
int sizeNeed = WideCharToMultiByte(CP_UTF8, 0, lpData2, MAX_PATH, lpData , 0 , NULL , NULL );
WideCharToMultiByte(CP_UTF8, 0, lpData2, MAX_PATH, lpData , sizeNeed, NULL, NULL);
#ifdef _WIN64
sprintf(lpDataTempClass2, "%s\\libn\\x64", lpData);
CreateDirectoryRecA( lpDataTempClass2 );
sprintf(lpDataTempClass2, "-Djava.library.path=%s\\bin;%s\\libn\\x64", lpchJavaHome, lpData );
#else
sprintf(lpDataTempClass2, "%s\\libn\\x86", lpData);
CreateDirectoryRecA(lpDataTempClass2);
sprintf(lpDataTempClass2, "-Djava.library.path=%s\\bin;%s\\libn\\x86", lpchJavaHome, lpData );
#endif
options[1].optionString = lpDataTempClass2;
//options[0].optionString = "-Djava.complier=JIT";
/* native lib path */
//"-Djava.library.path=c:\\mylibs"
//sprintf(lpchDataLib,"-Djava.library.path=%s",lpchLibrary);
options[2].optionString = "-Djava.complier=JIT";
pfnGetDefaultJavaVMOptions(&vm_args);
vm_args.version = JNI_VERSION_1_6;//JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 3;
vm_args.ignoreUnrecognized = TRUE;
if(pfnCreateJavaVM == NULL) {
return NULL;
}
if(!pfnGetDefaultJavaVMOptions) {
FreeLibrary(hinst_JVM);
return NULL;
}
//Create JVM
jint iRetval = pfnCreateJavaVM(&vm, (void**)&env,&vm_args);
//Error handling.
if (iRetval < 0) {
vm_args.nOptions = 2;
options[2].optionString = NULL;
iRetval = pfnCreateJavaVM(&vm, (void**)&env,&vm_args);
if(iRetval < 0) {
return NULL;
}
}
*env2 = env;
return vm;
}
from javacpp.
That works for Win32.
Also it can work on Linux and MAC too with some little work
from javacpp.
JavaCPP will make C++ code from Java class library.
Invoke Java from C++.
and wrap the class file with C++ methods
from javacpp.
jar files will be used a dll inside C++ can be good idea!
from javacpp.
I will give you example C++ code for that if you want to add such capability to JavaCPP
from javacpp.
As mentioned in the README.md
file, there's already solutions like Jace and JunC++ion for that. What is missing from those two?
from javacpp.
There's also jni.hpp these days: https://github.com/mapbox/jni.hpp
from javacpp.
Related Issues (20)
- Optional pair HOT 2
- ParserException for valid c++ code HOT 1
- Function pointer returning shared_ptr HOT 10
- LNK2001: Unresolved external, issue with namespace? HOT 7
- infinite recursion when calling virtualized inherited function HOT 4
- Mapping Template Constraints, Parser Error HOT 5
- Parser cannot compile because of QT HOT 4
- Template class with template functions HOT 15
- Inadequate Error Handling HOT 1
- `@StdVector` default annotation causing `Parser` to ignore templated constructors and operator overloads with `std::vector` arguments HOT 10
- Crash when passing a unique_ptr by value HOT 12
- Potential class loading deadlock when call static methods of Loader or Pointer HOT 13
- Multiple pointer types and automatic generation of function overloads HOT 1
- Parser doesn't add @Virtual annotations HOT 2
- A question about garbage collection on native objects HOT 3
- Pointer types don't work on the native side if allocated in java HOT 16
- Missing dependencies reported by gradle HOT 1
- Not found libs based on x86 in 1.5.10 HOT 4
- Marking an interface with virtualize() causes compilation failure when returning byte array
- Panama HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javacpp.