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.

uh, if it is already an .exe, i think you can look at this

uh, if it is already an .exe, i think you can look at this

I'm really not sure if the link you provided will be helpful, because in Java you cannot make native calls to C++ code directly (or any program that has potential access to memory) within an Applet context.

I'm assuming the original poster wants a way to invoke the executable program from Java (where instead of actually using the code within Java, the code is executed separately from the Applet).

This is a slightly advanced problem.

I would guess (and simply guess) that there is some way to run a script from Java that will inversely execute the file from its location. This could be done via an action command like a button and of course only happen once (in theory).

Edit: Time to eat my words! Looks like you can bypass the native-call restriction from an Applet using a technique at this link: http://www.javaworld.com/javaworld/jw-10-1998/jw-10-apptowin32.html

I'm really not sure if the link you provided will be helpful, because in Java you cannot make native calls to C++ code directly (or any program that has potential access to memory) within an Applet context.

I'm assuming the original poster wants a way to invoke the executable program from Java (where instead of actually using the code within Java, the code is executed separately from the Applet).

This is a slightly advanced problem.

I would guess (and simply guess) that there is some way to run a script from Java that will inversely execute the file from its location. This could be done via an action command like a button and of course only happen once (in theory).

Edit: Time to eat my words! Looks like you can bypass the native-call restriction from an Applet using a technique at this link: http://www.javaworld.com/javaworld/jw-10-1998/jw-10-apptowin32.html

i haven't really done anything with the JNI, or anything native with java in the slightest, i just googled calling C++ from java , and it turned up that link...

personally i find using native stuff a little counter-java-ish (for lack of a better way to put it) in the respect that java is supposed to be platform independent, and native stuff ruins that independence.
i
figured if i ever need to open calc for some reason from java i have a link to the way that it is done. and if his compiler is an .exe and all he needs to do is invoke it, it may be what he needs too.

not many people would contradict themselves in the same post, way to keep up to date with your information though (once you found out you were wrong you corrected yourself) i like that :) i try to do it too, but sometimes i don't find the contradiction in time to edit the post, on this site anyway.

AlEX is right , what i am asked to do is :-

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.

oh, so will this be a stand alone application or an applet, because if it isn't an applet you don't need to work around the security. you just need to invoke the compiler (with the text in the box as a parameter?).

yeah

My guide haven't told me about security, so it must be stand alone, just a 2-phase compiler running on one machine.

so how can i just call the C program by clicking the button?

is it already an exe?

I think it should be an an exe.

1. first i will write the C program with .c extension in turbo c++
2. compile it separately
3. save it into a file.

thus file must be called by clicking the button.

I have Textpad 5 installed on my machine. I'm trying to run it using the button within a JApplet but I'm getting an access denied error (as expected)

import javax.swing.*;
import java.awt.event.*;

public class TernaryTest extends JApplet implements ActionListener{

	private JButton button;

	public void init(){
		setSize(500, 500);
		button = new JButton("Execute Textpad");
		button.addActionListener(this);
		getContentPane().add(button);
	}

    public void actionPerformed(ActionEvent e){

		try{
			String WIN_PROGRAMFILES = System.getenv("programfiles");
			String FILE_SEPARATOR   = System.getProperty("file.separator");

			String[] commands =
			{"cmd.exe",
			"/c",
			WIN_PROGRAMFILES
			+ FILE_SEPARATOR
			+ "textpad 5"
			+ FILE_SEPARATOR + "textpad.exe"};
			Runtime.getRuntime().exec(commands);
		}catch(Exception f){System.out.println(f);}
	}
}

is still think that this may help but i don't know

Might help i will try and let you know.

is still think that this may help but i don't know

I had trouble loading the page so I only saw the first post - apparently our posts are similar after all.

The problem is that the code examples are given in applications and not within Applet context. I believe that you need to (somehow) make the Applet aware of your compiler when it is deployed as a Browser.

I've been looking through links all day only finding "Security issues with Applets," "How to call native code with signed Applets (again and again...)," and other useless links.

In no way am I saying it's impossible. I do believe it is possible without the need to sign your Applet - you simply need to find a way to make the Applet know that the program call is relative to the Server the Applet resides on, and not a Client.

I'll continue looking - hopefully a professional can shed some light on this issue, which seems to be a reoccurring one.

i think he is using an application and not an applet he did say that in one of his psots

i think he is using an application and not an applet he did say that in one of his psots

This is the first post from the original poster--

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.

My guide haven't told me about security, so it must be stand alone, just a 2-phase compiler running on one machine.

post #8

post #8

Talk about changing requirements! Looks like I need to pay more attention to the problem domain.

Hopefully a solution has been discovered.

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?

The whole thing is within the JNI............

oh, cannot include means it doesn't exist, you need to include yourjavapath/include in your compile of the cpp file i am not C++ savy, i rely on the IDE to do that, but being that this is your final project, you i figure you know how to do it.

hope this helps

i am trying but not getting the result.

you could try to copy that folder to your compilers include (i think) folder or whatever folder that it happens to be

as you can see i forgot to use:

CODE
static{
System.loadLibrary("compiler");
}


Now when i used it:

1.compiling with Javac gives no error.
2.But after i use Java compiler it gives:

Exception in thread "main" java.lang.unsatisfiedLinkError: no compiler in java.library.path
at java.lang.ClassLoader.loadlibrary(ClassLoader.java:1491)
java.lang.Runtime.loadLibrary0(Runtime.java:788)
java.lang.System.loadlibrary(System.java:834)
compiler.<clinit>(compiler.java:63)

my Java code was:

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();
}

now your entering stuff i don't know about, but if i were to guess i would say that you need to put whatever the compiler is in the same folder as your classes, but i haven't done anything with native code, so i think you should should wait for one of the professionals confirm.

sorry i'm of no more use

i am able to call the C program from the Java frame, but now i am facing another problem , things related to which i have posted in the C forum, pls visit that thread also.

thanks for your helps dudes!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.