Reputation: 4402
I have been reading many answers about the different between the compile time and the runtime in Java. But I am still not clear. Some answers said: the compile time is the period when you, the developer, are compiling your program or code. My question is when do I compile my program or code? For example: I open my IDE, eclipse or netbeans, write code in different classes and click on Run button and my application opens. Can someone explain me when did I compile my program/code in this sample process? or when was I in the compile time stage in this sample process?
Upvotes: 5
Views: 1211
Reputation: 75426
There is a very important thing you may not have fully understood yet, namely that the text you type - which in this case makes a Java program - is not on the form of the instructions that the CPU is executing millions of every second - which for Java is Java Byte Code, and which needs to be present for the JVM to execute your program.
The transformation of the Java source code you wrote into the corresponding Java Byte Code, is done by a so-called Java compiler. There is nothing magical about a compiler as it is just a program which can read in text and generate the corresponding byte codes, and it is a standard assignment for computer science students to write one (but usually for smaller languages than Java).
If you write your programs in a standard text editor (not an IDE) and save to disk, you need to manually invoke the Java compiler by running javac
on your Java sources. One of the advantages of IDE's is that they usually do the compilation automatically - either immediately when you save your file or when you want to run your program - but it makes it a bit more magical what goes on.
(Note: This transparent compilation step in an IDE becomes very useful when debugging in i.e. Eclipse, as it allows for updating the code being executed without having to restart the debug session from scratch.)
Upvotes: 1
Reputation: 4598
Do this. Open notepad. Type in :
class Sampl{
public static void main(String []args){
System.out.println("hi from run time");
}
}
Save it as Sampl.java
Save it in a new folder without spaces - say c:\j\academic or ~/j/academic if on linux
Now open a command promot, figure out path to your JDK and type in
cd c:\j\academic dir
Should see just Sampl.java
javac Sampl.java dir
Should see 2 files : Sampl.java and Sampl.class
That's you byte code
Now you can move or even delete Sampl.java and can still run Sampl.class from command line using
java -cp . Sampl
So you notepad and .java time was coding time. On command prompt was compile and run time
javac is the java compiler
java.exe is the runtime app that loads and runs our classes
[When runing jboss or other app container we run java with the jboss main class, and its calsses load and run ours]
These search results should help too google java tutorial command propmpt
Upvotes: 1
Reputation: 4923
When you write any java class,extension of file must be .java. Let take simple java class to print Hello World :
public class Simple {
public static void main(String[] args) {
System.out.println("Hello World !!");
}
}
So save this file as Simple.java.
Now open the cmd,lets say file saved in d:\test directory
d:\test>javac Simple.java // When you run this the .java is converted into byte code and it is saved in .class
file.
d:\test>java Simple // JVM will execute the byte code file i.e. Simple.class
Note
: All this process is done by IDE internally
Upvotes: 1