|  | |  |
11-30-2004, 11:49 AM
|
#1 (permalink)
| | Code Monkey
Join Date: Oct 2004
Posts: 51
| Compiling And Running 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 |
| |
11-30-2004, 11:52 AM
|
#2 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| well i've never done this, but it should be exactly like executing a system command.
to compile, use: javac file.java
to run, use: java file.class
i *think* this is accurate info ... bel might have to confirm.
the system or user PATH variable would have to be set so javac can be called from anywhere. i think javac is in the /bin/ path of your java sdk.
__________________ Mike |
| |
11-30-2004, 12:43 PM
|
#3 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| This is honestly over my head. You might want to see about hooking Ant into your program. There's also a Compiler class you can take a look at.
See about getting the SDK for Netbeans and see how they do it. |
| |
12-01-2004, 07:53 AM
|
#4 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 30
| Quote: |
Originally Posted by freesoft_2000
Does anyone know how to compile and run a java program from a program? | Is the program also written in Java?
See Runtime.getRuntime.exec(). http://java.sun.com/j2se/1.4.2/docs/...g/Runtime.html
In other languages, you'll have to look up how to call the system commands. C has a way to fork and pipe (unix C has an actual "system" command, windows probably also does, but I don't know the command name). C++ probably has a normal exec, similar to this. |
| |
12-01-2004, 10:01 AM
|
#5 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| exec() is a tricky proposition as I'm not sure it acts the same way on all systems. |
| |
12-02-2004, 08:25 AM
|
#6 (permalink)
| | Centurion Nova Prime
Join Date: May 2002 Location: Oak Park, IL (USA)
Posts: 287
| The only issue I ever had with exec() was the path differences between Windows and Unix/Linux. There is a system property that will indicate which OS you're running on. You can use this to determine the correct path structure. Having said that, it seems like there should be a better way than exec(), but with a little care, you can get your program (usign exec() ) to work properly on different OSes.
__________________ It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion. |
| |
12-04-2004, 12:03 PM
|
#7 (permalink)
| | Code Monkey
Join Date: Oct 2004
Posts: 51
| hi everyone,
I need to clear something with you guys
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 |
| |
12-04-2004, 03:20 PM
|
#8 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| I think so, but I almost never compile in a pure Windows environment, so I can't say for certain.
Honestly, if you want to compile in a Java program, I'd try to figure out how to plug in Ant. |
| |
12-10-2004, 01:38 PM
|
#9 (permalink)
| | Code Monkey
Join Date: Oct 2004
Posts: 51
| Hi everyone,
This is what i did 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 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
Last edited by Belisarius; 12-11-2004 at 09:24 AM.
|
| |
12-10-2004, 04:06 PM
|
#10 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| NullPointers are runtime exceptions, that is they only show up when you run the program; the compiler won't be able to catch them.
When you see the NullPointerException, it should give you some line numbers (although it might not). Can you show us what line the NullPointer is occuring at? Also, could you post the stack trace (the output from the exception)? |
| |
12-11-2004, 08:20 AM
|
#11 (permalink)
| | Code Monkey
Join Date: Oct 2004
Posts: 51
| Hi everyone,
I did say where the exception was occuring belizarius Code:
//The below command command line is where the exception
//occurs saying it is a null pointer exception
Process1 = Runtime1.exec(str11); I hope someone can help me with this
Yours Sincerely
Richard West |
| |
12-11-2004, 09:29 AM
|
#12 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| Sorry, I was in a bit of a rush when I posted that, and didn't notice the comments.
Anyways, according to the documentation the only reason the exec() would throw a NullPointerException would be that either the String array would be null, or one of the elements was null, but that doesn't seem to be the case. Could you post the stack trace you get? |
| |
12-12-2004, 09:11 AM
|
#13 (permalink)
| | Code Monkey
Join Date: Oct 2004
Posts: 51
| 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 Code:
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 |
| |
12-12-2004, 12:34 PM
|
#14 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,174
| I don't believe your environment is necessarily available to you when you access exec(); that is your classpath might not be set.
It sounds like what you should be looking into is class-loading. I don't know much about it myself, but it strikes me as a better way to load up bytecode than to attempt to execute it via exec(). Just google around for what you need. |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 01:28 PM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |