Hi everyone,

I am trying to compile some java codes from a .java file that i had saved to disk. Basically i am trying to build my own ide. I have a text area in which the java code is in and also have two buttons. What i need is when i click the button the .java file on my disk is compiled and any errors are shown in the textarea

For the second button when i click it(if compilation is successfull) for the .java that has been compiled to be run.

Does anyone know how to compile and run a java program from a program?

I hope someone can help or show me some codings on how this can be achieved.

Any amount of help is greatly appreciated

Thank You

Yours Sincerely

Richard West

Recommended Answers

All 13 Replies

TextPad is a freely available windows program that allows you to view your source code (the .java file) in one text area. There is an option in the tools menu to compile. The output from compiling is shown is different text area. There is another option on the tools menu to run your compiled code.

Basically java is compiled via the following command:
javac filename.java

The newly compiled code will have the name filename.class. It can now be run via the following command:
java filename

Any program, like textpad, can take advantage of these two commands to run java.

Hi everyone,

I am trying to compile some java codes from a .java file that i had saved to disk. Basically i am trying to build my own ide. I have a text area in which the java code is in and also have two buttons. What i need is when i click the button the .java file on my disk is compiled and any errors are shown in the textarea

For the second button when i click it(if compilation is successfull) for the .java that has been compiled to be run.

Does anyone know how to compile and run a java program from a program?

I hope someone can help or show me some codings on how this can be achieved.

Any amount of help is greatly appreciated

Thank You

Yours Sincerely

Richard West

hi everyone,

santa_barbarian you said
"Basically java is compiled via the following command:
javac filename.java"

let's say for example if my javac file is located in the file location
"C://abc/fd/javac.exe"

and the file i want to compile is located at "D://ac/d/lpc.java"

so when i compile the file i must writethe following on the command line

"C://abc/fd/javac D://ac/d/lpc.java"

Am i right or wrong. If i am wrong then what is the correct way to do it

Thank You

Yours Sincerely

Richard West

Hi everyone,

Here is what i have done so far but i always get the exception null pointer even though it is not empty but the program compiles with no errors

Here is part of the code

public void compile ()
{
     byte[] buffer1 = new byte[2048];
     int len = 0;
    String str9 = "C:ddk1.4/bin/javac";
     String str10 = "D:gui.java";
     String[] str11 = {str9, str10};
  File File11 = new File("C://JIDE_Errors.TXT");

try
{
       Runtime1 = Runtime.getRuntime();
//The below command command line is where the exception 
//occurs saying it is a null pointer exception

        Process1 = Runtime1.exec(str11);
          InputStream in = new BufferedInputStream(Process1.getErrorStream());
        FileOutputStream out = new FileOutputStream(File11);

while((len = in.read(buffer1)) != -1)
{
          out.write(buffer1, 0, len);
}

         Process1.waitFor();

if(Process1.exitValue() == 0)
{
              System.out.println("Compilation Successful");
}

else
{

}

}

Am i missing something. I really do not understand why there is a null pointer exception.

I hope someone can help me with this problem

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

NullPointerException is a RuntimeException which means you don't have to catch it in your code.
Therefore the compiler didn't complain.
Where did you get it? Figure that out and check whether whatever you're using there indeed isn't null before continuing.

If you don't know what a NullPointerException is I'd advise you to stick to simpler programs than what you're attempting to create for now.

P.S. you may want to take a look at java.lang.Compiler which seems to provide a wrapper around the compiler enabling you to avoid all that messy Runtime.exec() which is not a very nice way to do things at all.

Hi everyone,

i had a look at the java.lang.Compiler class and it seems to fit my needs but does java have a wrapper class that runs the compiled class as well. If the do where do i find the documentation for it.

Yours Sincerely

Richard West

You can execute any public method on any class from any other class.
So you could just call the main() method on your class.

Or more properly you'd instantiate another classloader and use that I guess.

Hi everyone,

Sorry to bother you guys but what if i want to run the class file that i have already compiled.

Let's say if my compiled class is located at C:/JButtons.class
and if i want to run this class this is what i did

String[] str12 ={"C:/j2sdk1.4.2_04/bin/java", "C:/JButtons"}; 

      Runtime Runtime1;
      Process Process1;

try
{
       Runtime1 = Runtime.getRuntime();
       Process1 = Runtime2.exec(str12);

       Process1.waitFor();
}

catch(Exception e)
{
       e.printStackTrace();
}

There is no exception thrown but the exit value of the process is 1 (meaning the program exited abnormally). I do not know what i am doing wrong. Am i running the compiled class the correct way. If not then what is the correct way of doing it. The program i trying to run has no errors when compiled.

I hope some someone can help me with this problem

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

you don't need to run the java runtime since you're already in it :)

Just load the class and call its main method.

Hi everyone,

How would i do that assuming that that the class file i have is say for example at this location C:/JButtons.class. Could you show me a simple example of doing this if it is of no inconvinience to you

Thank You

Yours Sincerely

Richard West

If you don't know how to call a static method on a class I suggest you get a beginners' book and start reading.

hi everyone,

I think you are misunderstanding my problem. I am thinking it has something to do with the classpath but i am not sure if i am setting it correctly in the below code snippet. I tried this( as an attempt to change the classpath)

String[] str12 ={rstr, "-cp.;", "C:/JButtons"}; 

// rstr is the location of my java intrepreter
// The value of rstr is C:/j2sdk1.4.2_04/bin/java
// C:/JButtons is the location of the class i want to run but can't
// seem to although the class exists at that location

       Runtime runtime1;
       Process process1;

try
{
       runtime1 = Runtime.getRuntime();
       process1 = runtime1.exec(str12);

       process1.waitFor();
}

catch(Exception e)
{
       e.printStackTrace();
}

What i think is that the problem has to do with the setting of my arguments in the str12 array and i maybe my setting of the class path is wrong but i am very sure of setting the classpath correctly
programatically

I hope someone can help me with this problem

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

Have you even read what I told you?
Something like this should do the trick quite nicely:

String[] args = new String[]{}; // fill with your arguments... Might need some experimentation to find the correct arguments 
Class.forName("JButton").getMethod("main", new Class[]{(String[]).getClass()}).invoke(null, args);

No need to launch another jvm inside your JVM.
You may wish to instantiate another ClassLoader, which would make things a tad more complicated but not a lot more.

Mind that you will need to handle quite a few (5 or 6) exceptions for that line of code.

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.