i have been given a 2-phase compiler containig the lexical phase and the syntax phase as my final year project. for that i need to know how to call a C program from a Java applet using a button called "compile". A little help will make me thankful.
1. Write a C program for the compiler.
2. Build an interface with JAVA.
3. Give some button, give a text area.
4. Write any thing in the text area.
5.when you click the "compile" button the C program must be called and it should compile the text in the text area.
My guide haven't told me about security, so it must be stand alone, just a 2-phase compiler running on one machine.
i have gone through "The Complete reference to Java- by Herbert Schildt"
1. There it say i must declare the method i want to call from the C program as:
public native int method();
we should not define the method here.
2. Produce a header to use in the C program by:
javah -jni <Java Program name>
3. Write the C program using:
a. # include <jni.h>
b.# include "programname.h" /* the header file we created"
c. # include <stdio.h>
4. As i am doing in Windows environment so:
to compile the C program i should use
Cl/LD <C programname.c>
BUT after compiling the error in am getting is:
fatal error C1083: Cannot open include file:'jni.h': No such files or directory
Can you help to solve this?
My Java code is:
java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class compiler extends Frame implements ActionListener{
TextArea A = new TextArea(32,140);
Button S = new Button("SAVE");
Button C = new Button("COMPILE");
Button Cl = new Button("CLEAR");
Button E = new Button("EXIT");
public static void main(String args[])
{
compiler f = new compiler();
f.setSize(900,900);
f.setVisible(true);
}
public compiler(){
setTitle("COMPILER");
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(A);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(S);
p2.addİ;
p2.add(Cl);
p2.add(E);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(p1);
add(p2);
S.addActionListener(this);
C.addActionListener(this);
Cl.addActionListener(this);
E.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
compiler f = new compiler();
String args = e.getActionCommand();
if(e.getSource() instanceof Button)
{
if(args.equals("SAVE"))
{
}
if(args.equals("COMPILE"))
{
f.test();
}
if(args.equals("CLEAR"))
{
A.setText("");
A.requestFocus();
}
if(args.equals("EXIT"))
{
System.exit(0);
}
}
}
public native void test();
}
and C program code is:
# include <jni.h>
# include "compiler.h"
# include <stdio.h>
JNIEXPORT void JNICALL Java_Compiler_test(JNIEnv *env, jobject obj)
{
printf("Testing!");
}
*edit: Please use code tags in the future, thanks!
This post has been edited by Martyr2: 3 Aug, 2008 - 10:50 AM