First Java Application: Setup and Execution

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What are the steps to prepare the first Java application?

  1. Install Java Software
  2. Select Java Editor
  3. Write Java Program
  4. Save Java File
  5. Compile Java File
  6. Execute Java Application

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?

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?

<p>If set all JAVA6, JAVA7, JAVA8 versions to &quot;path&quot; environment variable then Command prompt will take the java version which we provided as first one to the path environment variable in the order.</p> Signup and view all the answers

What is the purpose of Java Editor?

<p>Editor is a software, it will provide very good env to write java programs and to save java programs in our system.</p> Signup and view all the answers

Is it suggestible to use Editors in real-time application development?

<p>False (B)</p> Signup and view all the answers

What is Java API?

<p>API [Application Programming Interface</p> Signup and view all the answers

What are the two conditions to save a java file?

<p>1.If the present java file contains any public element[class, abstract class, interface, enum] then we must save java file with public element name only. If we voilate this condition then compiler will rise an error. 2.If no public element is identified in our java file then it is possible to save java file with any name like abc.java or xyz.java, but, it is suggestible to save java file with main() method class name.</p> Signup and view all the answers

Is it possible to provide more than one public class within a single Java file?

<p>False (B)</p> Signup and view all the answers

What is the main purpose of compiling a JAVA file?

<p>The main purpose of compiling JAVA file is to convert JAVA programme from High Level Representation to LowLevel Representation and to Check compilation errors.</p> Signup and view all the answers

To compile JAVA file, what command do we have to use on the command prompt from the location where JAVA File is Saved?

<p>javac File_Name.java</p> Signup and view all the answers

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

<p>javac -d target_location File_Name.java</p> Signup and view all the answers

Write the command to compile all the java files, which are available at current location

<p>javac *.java</p> Signup and view all the answers

If we want to execute java program then which command we have to use on command prompt?

<p>java Main_Class_Name</p> Signup and view all the answers

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?

<p>&quot;classpath&quot; environment variable</p> Signup and view all the answers

Flashcards

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?

To convert a JAVA program from High Level Representation to LowLevel Representation and to check compilation errors.

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?

The process by which the JVM accesses the main() method to start application execution by creating a 'Main thread'.

Signup and view all the flashcards

Java Editor Definition

Software that provides an environment to write java programs and save them in our system

Signup and view all the flashcards

Compiling Java Purpose

Convert Java program to low level representation.

Signup and view all the flashcards

JVM Main Function

JVM searches/loads Main_Class, then loads bytecode into memory.

Signup and view all the flashcards

JVM Execution Start

JVM accesses main() and runs application using the 'Main thread'.

Signup and view all the flashcards

Why set 'path'?

Set 'path' to make JDK tools like 'javac' and 'java' available to the OS.

Signup and view all the flashcards

Why set 'classpath'?

Set 'classpath' to tell JVM where to find main class '.class' files.

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 with javac 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, a java.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.

Quiz Team

Related Documents

More Like This

Java Application Programming Unit 4 Quiz
18 questions
Java Application Monitoring Quiz
8 questions
Use Quizgecko on...
Browser
Browser