PREREQUISITES:
# You will need the Sun Java Developer Kit (JDK) Java 1.6.0 or higher.
# Any C/C++ compiler installed.

USING THE CODE:

The JavaLauncher.cpp file contains the code that calls different functions from Java 
classes. Java Classes that are used in this project are given here under:

Old SensorFramework: de.uka.ipd.sdq.sensorframework/src/SensorFrameworkAPIDemo
New EDP2 Monitoring Framework: de.uka.ipd.sdq.edp2.examples/src/Edp2MeasurementExample

These classes contain the functions that will be called from the JavaLauncher.cpp. 

SensorFrameworkAPIDemo.java contain following functions that will be called 
from C/C++ code. 

  public static void main(String[] args){}

Edp2MeasurementExample.java contain following functions that will be called 
from C/C++ code. 

  static def Resource loadResourceFromFile(String fileName){}
  static def Resource save(String fileName, object){}
  static def File createDirectory(String path){}
  public def runExample() {}
  public def createDescription() {}
  public static void main(def args){}
  
To call these functions from C/C++ first you need to load the JVM using the 
following function:

JNIEnv* create_vm(JavaVM ** jvm) {
    
    JNIEnv *env;
    JavaVMInitArgs vm_args;

    JavaVMOption options; 
    //Path to the java source code     
    options.optionString = "-Djava.class.path=D:\\Java Src\\Test"; 
    vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
    vm_args.nOptions = 1;
    vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;
    
    int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
    if(ret < 0)
        printf("\nUnable to Launch JVM\n");       
    return env;
}        

Note that to use this code you will have to modify the 
options.optionString variable. You will have to set the path of the Java code. 
Where the Java classes are placed. Currently it being set to D:\Java Src\Test. 
You can modify accordingly. You will also need to modify the JDK version 
information in the above code as it is mentioned below:

vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6

Modify it if you have another JDK version installed.

To call a specific Java function from C you need to do the following

   1. Obtain the Class reference using the FindClass(,,) method.
   2. Obtain the Method IDs of the functions of the class that you want to call 
   using GetStaticMethodID and GetMethodID function calls.
   3. Call the functions using CallStaticVoidMethod, CallStaticIntMethod and 
   CallStaticObjectMethod.

One important thing to be noted here is specifying the function signatures 
while obtaining the method IDs.

To obtain the correct method signature you can use the following Java command.

javap -s -p SensorFrameworkAPIDemo
javap -s -p Edp2MeasurementExample

It will display you the signature of each function in specified class. 
These signature you can use to obtain the Method IDs. 
The result of above command can be seen below: 

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Program Files\Java\jdk1.6.0_16\bin>javap -s -p SensorFrameworkAPIDemo
Compiled from "SensorFrameworkAPIDemo.java"
public class SensorFrameworkAPIDemo extends java.lang.Object{
public SensorFrameworkAPIDemo();
  Signature: ()V
public static void main(java.lang.String[]);
  Signature: ([Ljava/lang/String;)V
}

Note that while specifying the Method name in the GetMethodID function 
if the Method is a constructor then its Method Name will be <init>.

HOW TO RUN:

To use this code follow the instructions below:

Compile the *.java files using javac command.

Compile JavaLauncher.cpp file using any C++ compiler.

CONVERTING THIS CODE TO PURE C FROM C++:

The attached code is written in C++. To convert this code into pure C you will 
have to modify following things in the JavaLauncher.cpp file.

Use

    (*env)->FunctionName(env,args,..); 

instead of

    env->FunctionName(args,..); 
    
    