Podcast
Questions and Answers
What are the steps to prepare the first Java application?
What are the steps to prepare the first Java application?
- Install Java Software
- Select Java Editor
- Write Java Program
- Save Java File
- Compile Java File
- Execute Java Application
What is the location where all JDK commands are existed after installation of JAVA software?
What is the location where all JDK commands are existed after installation of JAVA software?
C:\Java\JDK1.8.0\bin
What is the command to set the path on command prompt?
What is the command to set the path on command prompt?
set path=C:\Java\JDK1.8.0\bin;
If we set all three versions[JAVA6,JAVA7,JAVA8] to the path
environment variable then which version will come to the command prompt?
If we set all three versions[JAVA6,JAVA7,JAVA8] to the path
environment variable then which version will come to the command prompt?
What is the purpose of Java Editor?
What is the purpose of Java Editor?
Is it suggestible to use Editors in real-time application development?
Is it suggestible to use Editors in real-time application development?
What is Java API?
What is Java API?
What are the two conditions to save a java file?
What are the two conditions to save a java file?
Is it possible to provide more than one public class within a single Java file?
Is it possible to provide more than one public class within a single Java file?
What is the main purpose of compiling a JAVA file?
What is the main purpose of compiling a JAVA file?
To compile JAVA file, what command do we have to use on the command prompt from the location where JAVA File is Saved?
To compile JAVA file, what command do we have to use on the command prompt from the location where JAVA File is Saved?
Write the command to compile a java file from current location and send the generated .class files to some other target location on command prompt
Write the command to compile a java file from current location and send the generated .class files to some other target location on command prompt
Write the command to compile all the java files, which are available at current location
Write the command to compile all the java files, which are available at current location
If we want to execute java program then which command we have to use on command prompt?
If we want to execute java program then which command we have to use on command prompt?
If main class .class file is available at some other location then to make available main class.class file to JVM what we have to set?
If main class .class file is available at some other location then to make available main class.class file to JVM what we have to set?
Flashcards
What is a Java Editor?
What is a Java Editor?
A software that provides an environment to write and save Java programs.
What is the purpose of compiling a JAVA file?
What is the purpose of compiling a JAVA file?
To convert a JAVA program from High Level Representation to LowLevel Representation and to check compilation errors.
What is the role of the JVM?
What is the role of the JVM?
It takes the Main_Class name from the command prompt, searches for the Main_Class, and loads it into memory.
How does JVM start execution?
How does JVM start execution?
Signup and view all the flashcards
Java Editor Definition
Java Editor Definition
Signup and view all the flashcards
Compiling Java Purpose
Compiling Java Purpose
Signup and view all the flashcards
JVM Main Function
JVM Main Function
Signup and view all the flashcards
JVM Execution Start
JVM Execution Start
Signup and view all the flashcards
Why set 'path'?
Why set 'path'?
Signup and view all the flashcards
Why set 'classpath'?
Why set 'classpath'?
Signup and view all the flashcards
Study Notes
- The document outlines the steps to prepare and execute a first Java application
Steps to Prepare First Java Application
- Install Java Software
- Select a Java Editor
- Write a Java Program
- Save the Java File
- Compile the Java File
- Execute the Java Application
Installing Java Software
- Download the JDK (Java Development Kit) executable file from the internet, e.g., jdk-8-windows-i586.exe
- Double-click the downloaded .exe file to start the installation
- During installation, you might need to click "Yes," "Next," and "OK" buttons
- Change the JDK installation location to "C:\Java\jdk1.8.0" (or similar) by clicking "Change" and "OK"
- Change the JRE (Java Runtime Environment) installation location similarly, to "C:\Java\jre8" (or similar)
- After installation, set the "path" environment variable to include the JDK's bin directory (e.g., C:\Java\JDK1.8.0\bin)
- This makes Java commands available to the operating system
- You can temporarily set the path variable using the command prompt with:
set path=C:\Java\JDK1.8.0\bin;
- This change is only valid for the current command prompt session.
- To set the path permanently, use the following steps:
- Right-click "This PC" or "Computer" on the desktop
- Select "Properties"
- Select "Advanced System Settings"
- Click on the "Environment Variables" button
- In the "User variables" Section, click the "New".
- In the "Variable Name" field set the value to "path"
- Set the variable value to the JDK's bin directory (e.g., C:\Java\jdk1.8.0\bin;)
- Click "OK" on all open windows
Handling Multiple Java Versions
- If multiple Java versions (e.g., JAVA6, JAVA7, JAVA8) are set in the "path" environment variable, the system will use the first one in the order specified
- For example, if
path=C:\Java\jdk1.6.0\bin;C:\Java\jdk1.7.0\bin;C:\Java\jdk1.8.0\bin;
,java -version
will show JDK 1.6.0 - Batch files can simplify switching between Java versions
- Create a text file with the "path" command for a specific Java version, and save it with a ".bat" extension (e.g., java6.bat)
- To switch versions, open the command prompt, navigate to the directory containing the ".bat" file, and run the batch file (e.g., java6.bat)
Selecting a Java Editor
- A Java editor is a software used for writing and saving Java programs
- Examples include Notepad, Notepad++, and Editplus
- However, for real-time application development, using Integrated Development Environments (IDEs) like Eclipse, MyEclipse, or Netbeans is recommended
Writing a Java Program
- Use predefined libraries provided by the JAVA API (Application Programming Interface)
- A basic Java program structure includes:
class Test {
public static void main(String[] args) {
System.out.println("First Java Application");
}
}
Saving a Java File
- If the Java file contains a public element (class, abstract class, interface, enum), it must be saved with the same name as the public element (e.g. "Test.java" for
public class Test
) - Violating this condition will cause a compilation error
- If the Java file contains no public element, it may be saved with any name (e.g., abc.java).
- It is suggested to save the java file with the main() method class name.
Restrictions on Public Classes
- It's not possible to have more than one
public class
in a single Java file - If attempted, a compilation error will occur because the filename must match the public class name, which is impossible with multiple public classes
Compiling Java File
- The main purposes of compiling a JAVA file are to convert the JAVA program from a High-Level Representation to a Low-Level Representation, and check for compilation errors
- Use the command
javac File_Name.java
(e.g.,javac FirstApp.java
) in the command prompt - The operating system searches for the "javac" command in predefined locations specified by the "path" environment variable
- If the
javac
program is not found, an error message will appear; otherwise, the Java Compiler software is activated - The compiler searches for the specified .java file in the current location
- If the file is not found, an error message is displayed
- If the file is found, the compiler starts the compilation process from beginning to end
- If syntax errors are found during compilation, the compiler show error messages
- If no syntax errors are found, a
.class
file (or files) will be generated in the same directory - The number of
.class
files depends on the number of classes, abstract classes, interfaces, enums, and inner classes in the Java file - To compile from the current location but put the .class files in a different directory:
javac -d target_location File_Name.java
Package Declaration
- Using the
-d
option withjavac
is also used when you use package declaration statement in java file - The generated
.class
files for each member of the package will be stored in a directory structure w.r.t the package name - Consider the following example
package com.durgasoft.core;
enum E {
}
interface I {
}
abstract class A {
}
class B {
class C {
}
}
class FirstApp {
public static void main(String[] args) {
}
}
- If you use command:
javac -d c:\abc FirstApp.java
- The compiler will compile the file and generate E.class, I.class, A.class, B.class, B$C.class, FirstApp.class files to the location "C:\abc\com\durgasoft\core"
- You can compile all files in a directory by using:
javac *.java
, however, this will create the package directoy structure at your curent directory
Executing JAVA Application
- Use the command
java Main_Class_Name
(e.g.,java FirstApp
) to execute the program - The operating system will run the "java" program (the JVM)
- The JVM will then execute the following actions:
- JVM takes the Main_Class_Name from the command prompt
- JVM searches for that class in: current location, java libraries, locations in "classpath" variable
- If the .class file isn't found in those locations, either java.lang.NoClassDefFoundError (Java 6) or "Error: Could not find or load main class FirstApp" (Java 7+) will occur
- To make the .class file available from another location, set "classpath" (e.g.,
set classpath=E:\XYZ;
). - After finding the main class, JVM will load/read bytecode to the memoery using "Class Loaders".
- Next JVM will search for the
main()
method. - If the
main()
method is not found, ajava.lang.NoSuchMethodError:main
(Java 6) or an "Error: Main method not found in class FirstApp, please define main method as: public static void main(String args[])" (Java 7+) will be displayed - If the
main()
method exists, then JVM will create a thread called "Main thread" and execute the main() method - Once the main() method execution completes, the main thread is destroyed, and the JVM shuts down.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.