0% found this document useful (0 votes)
2 views5 pages

Java LectureNotes LaunchingProgram

The document explains how to launch single-file source-code programs in Java starting from JDK 11, allowing execution without prior compilation. It covers executing basic programs, passing arguments, defining multiple classes in a single file, referencing JDK and non-JDK classes, and executing as a shebang file on Unix-like systems. Additionally, it provides examples and command-line instructions for each method discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Java LectureNotes LaunchingProgram

The document explains how to launch single-file source-code programs in Java starting from JDK 11, allowing execution without prior compilation. It covers executing basic programs, passing arguments, defining multiple classes in a single file, referencing JDK and non-JDK classes, and executing as a shebang file on Unix-like systems. Additionally, it provides examples and command-line instructions for each method discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HaQT Fundamental Programming

Launching Single-File Source-Code Programs

1 Single-File Source-Code Program Execution

In JDK 11, Java introduced the ability to launch a single-file source-code program with the
java launcher, without first needing to explicitly compile the source code. This works by the
java launcher automatically invoking the compiler and storing the compiled code in-memory.
This can be a great way to learn how to use Java or explore new features within the Java
API, without having to go through the cruft of compiling and then executing code. There
are several ways to use this feature, as well as some limitations and things to keep in mind.

2 Executing Your First Single-File Source-Code Pro-


gram

To execute a single-file source-code program, the first class defined in the source file must
contain public static void main(String[]) like in HelloWorld below:

1 p u b l i c c l a s s HelloWorld {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 System . out . p r i n t l n ( ” H e l l o World ! ” ) ;
}
5 }

From the command line, HelloWorld can be launched with (accepting the name of the file is
also [Link]):

Command window

1 $ j a v a HelloWorld . j a v a

Passing in Arguments

1
HaQT Fundamental Programming

Arguments can also be passed in like with a normally compiled class, so in the below:

1 public c l a s s HelloJava {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 System . out . p r i n t l n ( ” H e l l o ” + a r g s [ 0 ] ) ;
}
5 }

Passing in an argument can be done like this:

Command window

1 $ j a v a H e l l o J a v a . j a v a World !

3 Multiple Classes in Same File

Multiple classes can be defined within the same source file if needed for encapsulation pur-
poses, like in this example:

1 public c l a s s MultipleClassesInSameFile {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 System . out . p r i n t l n ( GenerateMessage . g e n e r a t e M e s s a g e ( ) ) ;
System . out . p r i n t l n ( AnotherMessage . g e n e r a t e A n o t h e r M e s s a g e ( ) ) ;
5 }
}
7

c l a s s GenerateMessage {
9 s t a t i c String generateMessage ( ) {
r e t u r n ” Here i s one message ” ;
11 }
}
13

c l a s s AnotherMessage {
15 s t a t i c S t r i n g generateAnotherMessage ( ) {
r e t u r n ” Here i s a n o t h e r message ” ;

2
HaQT Fundamental Programming

17 }
}

When executed:

Command window

$ java MultipleClassesInSameFile . java

Will output:

Command window

1 Here i s one message


Here i s a n o t h e r message

4 Reference JDK Classes and Non-JDK Classes

A class that is part of the core JDK does not need to be added to the classpath to be executed.
So this example, referencing the Scanner and MatchResult classes, can be executed simply
with the java launcher:

import j a v a . u t i l . Scanner ;
2 import j a v a . u t i l . r e g e x . MatchResult ;

4 p u b l i c c l a s s ScannerExample {
p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g s ) {
6 S t r i n g wordsAndNumbers = ” ” ”
Longing r u s t e d f u r n a c e
8 daybreak 17 b e n i g n
9 homecoming 1
10 f r e i g h t car
””” ;
12

3
HaQT Fundamental Programming

t r y ( Scanner s c a n n e r = new Scanner ( wordsAndNumbers ) ) {


14 s c a n n e r . f i n d A l l ( ” b e n i g n ” ) . map( MatchResult : : group ) . f o r E a c h ( System .
,→ out : : p r i n t l n ) ;
}
16 }
}

To launch:

Command window

1 $ j a v a ScannerExample . j a v a

However the below example referencing RandomUtils, part of the Apache Commons Lang,
will need to have the [Link] added to the classpath at launch:

1 import o r g . apache . commons . l a n g 3 . RandomUtils ;

3 p u b l i c c l a s s ReferenceNonJDKClass {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 System . out . p r i n t l n ( RandomUtils . n e x t I n t ( ) ) ;
}
7

To launch:

Command window

java =cp / path / t o /commons=lang3 =3.12.0. jar ReferenceNonJDKClass . j a v a

4
HaQT Fundamental Programming

5 Executing as a Shebang File

On a Unix-like operating system, a single-file source-code application, can also be launched


as a shebang file like a script. Within the a java source file, as the first line in the file add
path/to/java/home –source <version> like in the below example:

Command window

1 #!/ path / t o / your / b i n / j a v a - - s o u r c e 16

3 public c l a s s HelloJava {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
5 System . out . p r i n t l n ( ” H e l l o ” + a r g s [ 0 ] ) ;
}
7 }

The file cannot have .java as its file extension, and must also be executable chmod +x. With
that, it can be launched with:

Command window

1 ./ HelloJava

You might also like