Runtime.exec()

hi.. i want to compile some commands in a file at runtime. i ll use Runtime runtime = Runtime.getRuntime(); method but i dont know what parameters i need. my files name is "tempfile" i need to compile commands in "tempfile". i want to learn how i can do this. thanks all help.....
[287 byte] By [aDiGea] at [2007-11-26 18:37:10]
# 1
What do you mean "compile some commands"? Are you talking about Java code here?
paulcwa at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 2
Runtime.getRuntime().exec("command string");
qUesT_foR_knOwLeDgea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 3

> hi.. i want to compile some commands in a file at

> runtime. i ll use Runtime runtime =

> Runtime.getRuntime(); method but i dont know what

> parameters i need. my files name is "tempfile" i need

> to compile commands in "tempfile".

Do you mean run command in tempfile. Is tempfile a batch file?

qUesT_foR_knOwLeDgea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 4

i have some java codes in my "tempfile" like;

import java.awt.*; //giris cikis

import javax.swing.*;

public class ikitamsayitoplaSWA extends JApplet

{

int toplam;

public void init()

{

int sayi1,sayi2;

toplam=0;

//

sayi1=Integer.parseInt(JOptionPane.showInputDialog(" Bir tam sayi giriniz : "));

sayi2=Integer.parseInt(JOptionPane.showInputDialog(" Ikinci bir tam sayi giriniz : "));

toplam=sayi1+sayi2;

}

public void paint(Graphics g)

{

g.drawRect(15,10,270,20);

g.drawString("iki sayinin toplami : "+toplam,25,25);

}

}

and i want to compile these codes which in my "tempfile.txt" at runtime with runtime.exec() or another method. but i dont know how i can do it...

aDiGea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 5
If you want to compile some java code, then put it in a file with the right name. Then invoke javac with Runtime.exec.
paulcwa at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 6

i am building these codes at runtime in my program. for example when user add a radibutton i am adding codes(applet code) for radiobutton in my "tempfile.txt". when user clicked "ok" i want to compile these codes whic is in my "tempfile.txt" and build an applet file.my program is building an applet. user building form and click "ok" then my program compile in "tempfile.txt" codes and create an applet file.

aDiGea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 7

i am sorry it was a bit complicate.lets make it simple. i have a txt file as "tempfile.txt" and there are these codes in it:

import java.awt.*; //giris cikis

import javax.swing.*;

public class ikitamsayitoplaSWA extends JApplet

{

int toplam;

public void init()

{

int sayi1,sayi2;

toplam=0;

//

sayi1=Integer.parseInt(JOptionPane.showInputDialog(" Bir tam sayi giriniz : "));

sayi2=Integer.parseInt(JOptionPane.showInputDialog(" Ikinci bir tam sayi giriniz : "));

toplam=sayi1+sayi2;

}

public void paint(Graphics g)

{

g.drawRect(15,10,270,20);

g.drawString("iki sayinin toplami : "+toplam,25,25);

}

}

in my program when user clicked a button i want to compile theses codes. i just need:

compile codes in "c:\tempfile.txt" ; its java code

thanks all inetersts...

aDiGea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 8
Am I missing something here? why does it need to be external at all?whats wrong with just using the current runtime and building the applet dynamically?
kikemellya at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 9

i am building a program for users who dont know java. they ll only add some components on the form, and i will create a file and add those components applet form. when building finished i ll compile those codes and built an applet. this program is for people who dont know building an applet with using java...

aDiGea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 10

Arguably you shouldn't be writing something like this until you know more about Java yourself.

But anyway, if you want to compile a class, then put it in a file with the appropriate name (not "tempfile.txt") and then compile it with javac, just like you do when you compile a class yourself, but this time invoked from Runtime.exec. (If you're using an IDE now, then stop it and start using command line tools while you do this project.)

I believe there are ways to hack into the compiler and invoke it directly, not using Runtime.exec, and possibly with more flexibility re: filenames and the like. But that would be a lot more difficult than just following naming conventions.

By the way: be forewarned that if you let others write code, you're opening the door to letting others write malicious code. What are you going to do with these classes once they're compiled?

paulcwa at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 11
And by the way, when you post code on these forums please wrap them in [code][/code] tags so they're easier to read.
paulcwa at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 12

i think you didnt understand me clearly, may be it is my fault.. i need explain my problem. i am building a program with java. in my program users add some components on the form(not in java, in my program which is built with java). and when they add a component (radiobutton,checkbox,button) i am building a tempfile ("tempfile.txt") and write that components applet code. after b齣lding form i want to compile these codes in "tempfile.txt". i want to do this in my program which is built with java. and i just need this direction's java code

compile "tempfile.txt" which is located in c:\tempfile.txt

i think i can do it with runtime() method if you know another method which is better it is ok.

i wish you could understand clearly...

aDiGea at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 13

This is out of some code that I used last week:

String[] cmd = {"cmd.exe" , "/c" ,"start" ,"vbMailer.vbs", email, accountNumber};

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);

int exitVal = proc.waitFor()

it works fine, just modify it.

I still don't think you need to do it the way you are. Imagine you have a blank Jpanel and you add to this when users fire events (i.e. add a JButton, or radio) and set it to visible or build a new class on the the fly when they click 'ok' or something.

kikemellya at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 14

> i think you didnt understand me clearly, may be it is

> my fault.. i need explain my problem. i am building a

> program with java. in my program users add some

> components on the form(not in java, in my program

> which is built with java). and when they add a

> component (radiobutton,checkbox,button) i am building

> a tempfile ("tempfile.txt") and write that components

> applet code.

Yes. I understand that perfectly, and I'm telling you DON'T CALL THE FILE TEMPFILE.TXT.

The only part I don't get (which you haven't explained) is how exactly the user input is selected, interpreted, and converted to Java code.

But that doesn't effect my previous warning: BE CAREFUL ABOUT MALICIOUS CODE INJECTION.

> after b齣lding form i want to compile

> these codes in "tempfile.txt". i want to do this in

> my program which is built with java. and i just need

> this direction's java code

>

> compile "tempfile.txt" which is located in

> c:\tempfile.txt

>

> i think i can do it with runtime() method if you

> know another method which is better it is ok.

>

> i wish you could understand clearly...

I do understand clearly. The problem is that you don't seem to understand my answers.

What part of "don't call the file 'tempfile.txt', give it a name following Java standards, and then use Runtime.exec to invoke javac, the compiler" don't you understand?

paulcwa at 2007-7-9 6:11:14 > top of Java-index,Java Essentials,Java Programming...
# 15

my problem is not about "tempfile.txt". i can change it, it is just a name. i just want to know that how can i compile some codes in my "anothername.txt"(it is ok, i think).

String[] cmd = {"cmd.exe" , "/c" ,"start" ,"vbMailer.vbs", email, accountNumber};

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);

int exitVal = proc.waitFor()

i just need these code's explain.my file is located in c:\ and how must i create a string.i dont know which parameters i have to use...

String[] cmd = [u]{i think i just need here};[/u]

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);

int exitVal = proc.waitFor()

Message was edited by:

aDiGe

Message was edited by:

aDiGe

aDiGea at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 16

> my problem is not about "tempfile.txt". i can change

> it, it is just a name. i just want to know that how

> can i compile some codes in my "anothername.txt"(it

> is ok, i think).

> > String[] cmd = {"cmd.exe" , "/c" ,"start"

> ,"vbMailer.vbs", email, accountNumber};

>

> Runtime rt =

> Runtime.getRuntime();

> Process proc = rt.exec(cmd);

> int exitVal = proc.waitFor()

> i just need these code's explain.my file is located

> in c:\ and how must i create a string.i dont know

> which parameters i have to use...

> [code]

> String[] cmd = {[u]i think i just need here[/u]};

>

> Runtime rt =

> Runtime.getRuntime();

> Process proc = rt.exec(cmd);

> int exitVal = proc.waitFor()

>

> Message was edited by:

> aDiGe

> was edited by:

>aDiGe

like this you mean?

String[] cmd = {"cmd.exe" , "/c" ,"javac" ,"myfilehere"};

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);

int exitVal = proc.waitFor()

kikemellya at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 17

yes i think but it doesnt work:

String[] cmd = {"cmd.exe" , "/c" ,"javac" ,"c:\\myfile.txt"};

does this code compile "myfile.txt".

i tried but it doesnt work.......

aDiGea at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 18

> yes i think but it doesnt work:

> > String[] cmd = {"cmd.exe" , "/c" ,"javac"

> ,"c:\\myfile.txt"};

>

>

> does this code compile "myfile.txt".

>

> i tried but it doesnt work.......

Hint - try compiling a .java file!

What were the symptoms of 'it doesnt work' ?

sabre150a at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 19
As far as I know it should, maybe someone can confirm?does it not have to be myFile.java? also are your java paths set up correctly. did you get any errors, a dos pop up or anything
kikemellya at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 20

ohhhh god!!! i am new on java and i have to admit that my english is not perfect, you can't understand that how it is difficult to explain and solve some problems. i want to write my whole code here, i dont have any error message it just doing nothing...

import java.awt.*;

import java.awt.event.*;

import java.io.IOException;

import javax.swing.*;

public class test extends JFrame implements ActionListener {

public test(){

JButton but=new JButton("click");

Container c=getContentPane();

c.setLayout(new FlowLayout());

but.addActionListener(this);

c.add(but);

}

public void actionPerformed(ActionEvent evt){

String[] cmd = {"cmd.exe" , "/c" ,"javac" ,"C:\\Documents and Settings\\All Users\\test\\exec.java"};

Runtime rt = Runtime.getRuntime();

try{

Process proc = rt.exec(cmd);

try{

int exitVal = proc.waitFor();}

catch(InterruptedException e){}}

catch(IOException e){}

}

public static void main(String[] args) {

test pen=new test();

pen.setSize(300,300);

pen.setVisible(true);

}

}

thanks for all interests.................

aDiGea at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 21

You should read the standard error and standard output streams of the process after you create it.

The chances are very good that javac is running, but is encountering errors with the java file. In fact, given your project, that's going to happen again and again (it's hard writing code that correctly generates other code, especially when it's based on unpredictable input).

Look at the API docs for Process. In particular look at getErrorStream and getInputStream.

And read this:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

paulcwa at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...
# 22
you can use Runtime rt = Runtime.getRuntime("your command");but if you want to open a program like firefox you must but the .exe file for firefox in the same folder, that has "tempfile.java" ....
java.oldnicka at 2007-7-21 17:26:13 > top of Java-index,Java Essentials,Java Programming...