A Java entry-point class has a main method with the following signature and modifiers:
public static void main(String[] args)
Sidenote : because of how arrays work, it can also be (String args[])
When the java command starts the virtual machine, it loads the specified entry-point classes and tries to find main. If successful, the arguments from command line are converted to Java String objects and assembled into an array. If main is invoked like this, the array will not be null and won't contain any null entries.
A valid entry-point class method must do the following:
It is conventional to declare the class as public but this not strictly necessary. From Java 5 onward, the main method's argument type may be a String varargs instead of a string array. main can optionally throw exceptions, and its parameter can be named anything, but conventionally it is args.
From Java 8 onwards the java command can also directly launch a JavaFX application. JavaFX is documented in the JavaFX tag, but a JavaFX entry-point must do the following:
This example covers common errors with using the 'java' command.
"Command not found"
If you get an error message like:
java: command not found
when trying to run the java command, this means that there is no java command on your shell's command search path. The cause could be:
Refer to "Installing Java" for the steps that you need to take.
"Could not find or load main class"
This error message is output by the java command if it has been unable to find / load the entry-point class that you have specified. In general terms, there are three broad reasons that this can happen:
Here is a procedure to diagnose and solve the problem:
If checking the classpath by hand did not find the issue, you could add the -Xdiag and -XshowSettings options. The former lists all classes that are loaded, and the latter prints out settings that include the effective classpath for the JVM.
Finally, there are some obscure causes for this problem:
"Main method not found in class <name>"
This problem happens when the java command is able to find and load the class that you nominated, but is then unable to find an entry-point method.
There are three possible explanations:
This is a continuation of the "main class" and "executable JAR" examples.
Typical Java applications consist of an application-specific code, and various reusable library code that you have implemented or that has been implemented by third parties. The latter are commonly referred to as library dependencies, and are typically packaged as JAR files.
Java is a dynamically bound language. When you run a Java application with library dependencies, the JVM needs to know where the dependencies are so that it can load classes as required. Broadly speaking, there are two ways to deal with this:
For an executable JAR file, the runtime classpath is specified by the "Class-Path" manifest attribute. (Editorial Note: This should be described in a separate Topic on the jar command.) Otherwise, the runtime classpath needs to be supplied using the -cp option or using the CLASSPATH environment variable.
For example, suppose that we have a Java application in the "myApp.jar" file whose entry point class is com.example.MyApp. Suppose also that the application depends on library JAR files "lib/library1.jar" and "lib/library2.jar". We could launch the application using the java command as follows in a command line:
Alternative 1 (preferred) java -cp myApp.jar:lib/library1.jar:lib/library2.jar com.example.MyApp # Alternative 2 export CLASSPATH=myApp.jar:lib/library1.jar:lib/library2.jar java com.example.MyApp
(On Windows, you would use ; instead of : as the classpath separator, and you would set the (local) CLASSPATH variable using set rather than export.)
While a Java developer would be comfortable with that, it is not "user friendly". So it is common practice to write a simple shell script (or Windows batch file) to hide the details that the user doesn't need to know about. For example, if you put the following shell script into a file called "myApp", made it executable, and put it into a directory on the command search path:
#!/bin/bash #The 'myApp' wrapper script export DIR=/usr/libexec/myApp export CLASSPATH=$DIR/myApp.jar:$DIR/lib/library1.jar:$DIR/lib/library2.jar java com.example.MyApp
then you could run it as follows:
myApp arg1 arg2 ...
Any arguments on the command line will be passed to the Java application via the "$@" expansion. (You can do something similar with a Windows batch file, though the syntax is different.
The java command supports a wide range of options:
Setting system properties with -D
The -D<property>=<value> option is used to set a property in the system Properties object. This parameter can be repeated to set different properties.
Memory, Stack and Garbage Collector options
The main options for controlling the heap and stack sizes are documented in Setting the Heap, PermGen and Stack sizes. (Editorial note: Garbage Collector options should be described in the same topic.)
Enabling and disabling assertions
The -ea and -da options respectively enable and disable Java assert checking:
The options can be combined. For example.
# Enable all assertion checking in non-system classes java -ea -dsa MyApp # Enable assertions for all classes in a package except for one. java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat MyApp
Note that enabling to assertion checking is liable to alter the behavior of a Java programming.
Selecting the VM type
The -client and -server options allow you to select between two different forms of the HotSpot VM:
By default, the JVM will run in 64bit mode if possible, depending on the capabilities of the platform. The -d32 and - d64 options allow you to select the mode explicitly.
1 - Check the official manual for the java command. Sometimes a standard option is described as "subject to change".
First of all, the problem of handling spaces in arguments is NOT actually a Java problem. Rather it is a problem that needs to be handled by the command shell that you are using when you run a Java program.
As an example, let us suppose that we have the following simple program that prints the size of a file:
import java.io.File; public class PrintFileSizes { public static void main(String[] args) { for (String name: args) { File file = new File(name); System.out.println("Size of '" + file + "' is " + file.size()); } } }
Now suppose that we want print the size of a file whose pathname has spaces in it; e.g. /home/steve/Test File.txt. If we run the command like this:
java PrintFileSizes /home/steve/Test File.txt
the shell won't know that /home/steve/Test File.txt is actually one pathname. Instead, it will pass 2 distinct arguments to the Java application, which will attempt to find their respective file sizes, and fail because files with those paths (probably) do not exist.
Solutions using a POSIX shell
POSIX shells include sh as well derivatives such as bash and ksh. If you are using one of these shells, then you can solve the problem by quoting the argument.
java PrintFileSizes "/home/steve/Test File.txt"
The double-quotes around the pathname tell the shell that it should be passed as a single argument. The quotes will be removed when this happens. There are a couple of other ways to do this:
java PrintFileSizes "/home/steve/Test File.txt"
Single (straight) quotes are treated like double-quotes except that they also suppress various expansions within the argument.
java PrintFileSizes /home/steve/Test\ File.txt
A backslash escapes the following space, and causes it not to be interpreted as an argument separator.
For more comprehensive documentation, including descriptions of how to deal with other special characters in arguments, please refer to the quoting topic in the Bash documentation.
Solution for Windows
The fundamental problem for Windows is that at the OS level, the arguments are passed to a child process as a single string (source). This means that the ultimate responsibility of parsing (or re-parsing) the command line falls on either program or its runtime libraries. There is lots of inconsistency.
In the Java case, to cut a long story short:
For more detail, please refer to the Batch-File documentation.
Executable JAR files are the simplest way to assemble Java code into a single file that can be executed. *(Editorial Note: Creation of JAR files should be covered by a separate Topic.) *
Assuming that you have an executable JAR file with pathname <jar-path>, you should be able to run it as follows:
java -jar <jar-path>
If the command requires command-line arguments, add them after the <jar-path> For example:
java -jar <jar-path> arg1 arg2 arg3
If you need to provide additional JVM options on the java command line, they need to go before the -jar option. Note that a -cp / -classpath option will be ignored if you use -jar. The application's classpath is determined by the JAR file manifest
When an application has not been packaged as an executable JAR, you need to provide the name of an entry-point class on the java command line.
Running the TutorJoes class
The "TutorJoes" example is described in Creating a new Java program . It consists of a single class called TutorJoes which satisfies the requirements for an entry-point.
Assuming that the (compiled) "TutorJoes.class" file is in the current directory, it can be launched as follows:
java TutorJoes
Some important things to note are:
Specifying a classpath
Unless we are using in the java -jar command syntax, the java command looks for the class to be loaded by searching the classpath; see The Classpath. The above command is relying on the default classpath being (or including) the current directory. We can be more explicit about this by specifying the classpath to be used using the -cp option.
java -cp . TutorJoes
This says to make the current directory (which is what "." refers to) the sole entry on the classpath.
The -cp is an option that is processed by the java command. All options that are intended for the java command should be before the classname. Anything after the class will be treated as an command line argument for the Java application, and will be passed to application in the String[] that is passed to the main method.
(If no -cp option is provided, the java will use the classpath that is given by the CLASSPATH environment variable. If that variable is unset or empty, java uses "." as the default classpath.)
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions