< Zurück | Inhalt | Weiter >

5.4.1 The Basics

You run a Java program by invoking the java command. Usually, the argument to the command is a class name. That class is loaded, and its main() method is run.

Remember, this is not a filename, but a class name! The rest of the com- mand-line arguments that follow the class name are passed as an array of Strings to the main() method of the named class. Example 5.4 demonstrates running the FetchURL program.



image

Example 5.4 Running a Java program with java

$ java FetchURL http://www.yahoo.com/news http://www.yahoo.com/news:

<html>

<head><title>Yahoo! Directory News and Media</title>

<script type="text/javascript" src="http://us.js1.yimg.com/us.yimg.com/lib/common/yg_csstare.js">

</script>

<style>

li { font-size: 12px; margin-bottom: 2px; }

</style>

<base href=http://dir.yahoo.com/News_and_Media/></head>

<body>

...

... etc.


image




CAUTION

Remember that if you override the classpath, either with the -classpath or

-cp command-line options (detailed below) or with the CLASSPATH environ- ment variable, you must include the “.” directory somewhere in the classpath if you want Java to include current working directory in the search list. Since “.” is the default classpath, many people are surprised when they set a classpath and suddenly can no longer run a .class file in their current directory.

image


The class invoked must be findable on either the bootstrap or user- specified classpath.