Sei sulla pagina 1di 7

Search this Website or visit homepage www.

michael-
thomas.com
Email Michael Thomas: <myfirstname>@michael-
thomas.com
©JCrazy LLC; ©Michael Thomas

Create and Compile Your First Java Program


Using a Text Editor and DOS
(Free Web Tutorials & Resources)
by Michael Thomas

Java First Steps Home Page

If you have trouble with any of the steps below see the table below called "Help Notes".

Steps
(Tested with Java SE 8u111)
Step: Using a Text Editor

Pick a text editor to use:

• Windows NotePad
◦ Windows comes with NotePad. You can use it to complete this section.
• NotePad++ - https://notepad-plus-plus.org/download - free & GPL license, written in C++
◦ I really like this editor.
◦ Download and install the .exe.
Note: There is a .zip file which is nice if you don't have rights to install a program.
• My Notes on other Text Editors

Step 1: DOS: Create some batch files to test JDK install

• Create a new directory to place the files you will work with.
You can do this using Windows Explorer or using DOS (click here for Help with DOS)
• You can use the Windows program "Notepad" to create these files.
• MyFirst_java_test.bat - create and run this batch file (click on it in Win Explorer).
Reason: Test your JDK install: java.exe, javac.exe, javaw.exe, appletviewer.exe
rem Optional:
rem * This is not needed if you set the JAVA_HOME and PATH system environment variables as in the tutorial.
rem call MyFirst_jdk_setpath.bat
echo off
echo.
echo **** Test for: java.exe (Run Java Applications)****
echo.
java.exe -fullversion
echo.
echo **** Test for: javac.exe (Compliler) ****
echo.
javac.exe -help
echo.
echo **** Test for: javaw.exe (Run Java Applications)****
echo.
javaw.exe -help
echo.
echo **** Test for: appletviewer.exe (Compliler) ****
echo.
appletviewer -help
pause

• Do not proceed unless you can run the previous batch file without errors.

Example of an Error:
'javac.exe' is not recognized as an internal or external command,
operable program or batch file.

• Optional (for possible reference only): MyFirst_jdk_setpath.bat


rem **********************
rem Note
rem **********************
rem On your personal PC system environment variables as in the tutorial instructions:
rem * JAVA_HOME
rem * PATH - add %JAVA_HOME%\bin
rem Other Option:
rem * Manually set the path. Just uncomment the line below.
rem path=%path%;"c:\_data\java\jdk1.8.0_111\bin"

Step 2: DOS: Create and compile your first Java Application

• Create, compile and Run your first Java Application. Java Applications are designed to on a local machine which has a
JVM (Java Virtual Machine) loaded. By the way, Java Servlets work the same way.
◦ Create: MyFirstApplication.java - the source code for the java application program.
/*
File.......: MyHello_Application.java
Description: Basic Application example.
Programmer.: Michael Thomas, 1/17/99
Compile....: javac MyHello_Application.java
Run........: java MyHello_Application

*/
public class MyFirstApplication {
public static void main( String [] args ) {
System.out.println("Hello World from an Application.");
}
}

◦ Create: MyFirstApplication_compile.bat - This will compile the .java source code and create a .class file.
rem call MyFirst_jdk_setpath.bat
javac MyFirstApplication.java -classpath .
pause

◦ Next, in Windows Explorer click on the bat file to run it.


In the DOS compile window, you will see the "javac" command. Wait until you see the "pause" command. There
should be no errors. Now press the "Enter" key.
Note: You just created the file: MyFirstApplication.class
◦ Create: MyFirstApplication_run.bat
rem call MyFirst_jdk_setpath.bat
java MyFirstApplication
pause

◦ Next, in Windows Explorer click on the bat file to run it.


This bath file will call the command "java" and execute the .class file you created when you compiled the .java
source.
You should see on the DOS console the words:

Hello World from an Application

Step 3: Create and compile your first Java Applet

• Create, compile and Run your first Java Applet.


◦ Java Applets are designed to run in a browser but can be tested and run from an exe installed with the Java JDK
called "appletviewer.exe".
◦ Create: MyFirstApplet.java - Java source code.
/*
File.......: MyHello_Applet.java
Description: Basic Applet example. (Run by a browser.)
Programmer.: Michael Thomas
Date.......: 1/17/99
Compile....: Myjavac Hello_Applet.java -d bin (dir .\bin)
Run........: Internet Browser or appletviewer Hello_Applet.html

*/
import java.applet.Applet;
import java.awt.Graphics;

public class MyFirstApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello World from an Applet. (MyFirstApplet.java)",25,25);
}
}

◦ Create: MyFirstApplet_compile.bat - This will compile the .java source code and create a .class file.
rem call MyFirst_jdk_setpath.bat
javac MyFirstApplet.java
pause

◦ Next, in Windows Explorer click on the bat file to run it.


In the DOS compile window, you will see the "javac" command. Wait until you see the "pause" command. There
should be no errors. Now press the "Enter" key.
Note: You just created the file: MyFirstApplet.class
◦ Create: MyFirstApplet.html - this is the HTML file that the browser will run.
<html>
<head>
<title>Hello World - Applet only</title>
<link rel="stylesheet" type="text/css" href="logofiles/stylesheet_webtutorials.css">
</head>
<body>
<h1 align="center">Hello World - Applet</h1>
<hr align="center">
<p align="center">
<applet code="MyFirstApplet.class" align="baseline" width="300" height="55">
</applet>
</p>
<hr align="center">
</body>
</html>

◦ For now we will create a batch file to use Java's appletviewer.exe to display the applet.
We are doing this because of additional browser security issues when running applets.
◦ Create: MyFirstApplet_view.bat - this will run a Java Applet using JDK's appletviewer.exe
rem call MyFirst_jdk_setpath.bat
appletviewer MyFirstApplet.html
pause

◦ Next, in Windows Explorer click on the bat file to run it.


This will use appletviewer.exe to display just the applet that is located in the file "MyFirstApplet.html". In the
appletviewer window you should see the words:
"Hello World from an Applet"
• Run a Java Applet in an HTML file.
◦ Because of Browser security issues we need to allow the browser to run the applet.
◦ First, let's see the issue.
The HTML code will display the following:
* Hello World - Applet - in a large font <h1>
* 2 horizontal lines.
Issue: The Java Applet is supposed to display the following words in between the 2 horizontal lines.
* Hello World from an Applet. (MyFirstApplet.java)
◦ See the issue at a web site:
http://www.michael-thomas.com/tech/java/javafirststeps/MyFirstApplet.html
Note: You see the HTML code but not the words from the Java Applet!
◦ See the issue on your local:
MyFirstApplet.html - click on the file with Windows Explorer.
Note: You see the HTML code but not the words from the Java Applet!
◦ Read this Error info. You will probably have to do this once you enable Java for a URL or Hostname.
◾ Error: Java(TM) was blocked because it is out of date and needs to be updated
Options: "Update" "Run this time"
◾ Click "Update" and follow the prompts to uninstall your old JRE and install an updated version.
◦ Enable Java Applets to run in a browser
◾ For security reasons, applets have to be enabled to run in a browser. You can enable for a specific file,
directory or hostname.
Once you have completed the steps try running the HTML file in the following browsers:
Internet Explorer
Firefox
(Note: Do not try Microsoft Edge!)
◾ Java's Help Page as of 10/18/2016
https://www.java.com/en/download/help/win_controlpanel.xml
This article applies to Platform(s): Windows 10, Windows 2008 Server, Windows 7, Windows 8, Windows
Server 2012, Windows Vista, Windows XP
◾ Win 10
◾ https://www.java.com/en/download/faq/win10_faq.xml
Will Java run in my browser on Windows 10?
Internet Explorer 11 and Firefox will continue to run Java on Windows 10.
The Edge browser does not support plug-ins and therefore will not run Java.
◾ In the "Ask me anything" section, type: Configure Java
Run the "Configure Java" desktop app.
◾ Click on the "Security" tab
◾ In the section "Exception Site List" click "Edit Site List"
Add the following:
http://www.michael-thomas.com/tech/java/javafirststeps/MyFirstApplet.html
(Note: Next is my example. Change to match your install!)
file:///C:\_data\webs\michael-thomas.com\tech\java\javafirststeps\MyFirstApplet.html
◾ Launch "Internet Explorer" (not IE's Edge!). In the "Ask me anything" search for "Internet Explorer".
Now go to the URL(s) that you enabled Java Applets to run.
◾ Win 7
◾ Click the windows "Start", "Control Panel"
◾ In the upper right corner search box type: Java Control Panel
◾ Click on the "Java" icon.
◾ Click the "Security" tab
◾ Select "High"
◾ Click "Edit Site List" and add the full pathname to the file:
Ex (web site): http://www.michael-thomas.com/tech/java/javafirststeps/MyFirstApplet.html
Ex (local file): file:///C:\<yourdirectory>\MyFirstApplet.html
Ex (web site, if you just the whole domain): http://www.michael-thomas.com
◾ Test launching the applet by clicking on the HTML file in windows explorer.
You can also test running from my website:
http://www.michael-thomas.com/tech/java/javafirststeps/MyFirstApplet.html

Step 4: Pat Your Self on the Back. Your finished!


Help Notes
(Sorry, could be outdated! I did not want to remove.)

Topic Notes
Environment DOS Environments:
Variables:
View the DOS Environment Variables.
• Path
• Classpath • c:\> set | more - This DOS command will show you all of the DOS environment variables and
page break if neccessary.

Changing the DOS Environment Variables.

• set CLASSPATH=. - This sets the class path to the current directory. Make sure you don't
add extra spaces to this command!
• set CLASSPATH= - This will remove the CLASSPATH variable.

DOS Variables: path & CLASSPATH

• path - shows the director order in which DOS will look for DOS Commands. I like to have the
JDK be the first in the list if your OS allows it. (ex: c:\jdk1.3\bin; c:\winnt; etc...)
• CLASSPATH - a variable that tells java which directories to look for classes. You should have
".;" be the first in the list. If you need to customize the CLASSPATH for a program use the -
classpath compile option when compiling (ex: javac HelloWorld.java -classpath . ).

General Notes - Environment Variables:

• User Variables Section


Note: Because some software programs create errors when installing, I always change the
setttings for "temp" and "tmp".
From: %USERPROFILE%\Local Settings\Temp
To: c:\temp (make sure the directory exits)

Win XP

• Start, Settings, Control Panel, System


• Click the tab "Advanced", then click "Environment Variables"

WinNT (NT4.0 & NT2000)

• Start, Settings, Control Panel, System, Environment


• Next Click on the "System Variable" called "Path".
• Next modify the "value" field by putting the path at the end of the current value. (Separate the
paths with a ";").
Ex: ....; c:\jdk1.3\bin

Win 95/98

• Steps
◦ Press Start, Run
◦ type: msconfig
◦ Then hit the "Enter" key.
◦ Click on the Autoexec.bat tab
◦ Click on "set Path" line. Then click "Edit"
◦ Right now you are viewing the Autoexec.bat file. Be careful while you are in this file.
◦ See if you have a command that begins with
"SET PATH =". If you do, add the correct path as the first entry after the "SET PATH
=". Choose from the following:
◾ For JDK 1.2 add: c:\jdk1.2\bin;
◾ For SDK 1.3 add: c:\jdk1.3\bin;
Note: If you don't have a "SET PATH" statement, add the following:
◾ set path = c:\jdk1.3\bin;
◦ Next, you must reboot.
◦ After you reboot go to a DOS prompt and type:
path
◦ If c:\jdk1.3\bin appears first your are completed. If c:\windows and/or
c:\windows\command is first, you maybe calling java.exe from those directories.

Win ME

• Steps
◦ Press Start, Run
◦ type: msconfig
◦ Then hit the "Enter" key.
◦ Click on the Environment tab
◦ Click on "Path" line. Then click "Edit"
◦ Be careful and add the correct path. Note - add the path as the first entry.
For JDK 1.2 add: c:\jdk1.2\bin;
For SDK 1.3 add: c:\jdk1.3\bin;
Ex: c:\jdk1.3\bin; c:\windows; c:\windows\command;
◦ Click on "Apply"
◦ Now click on the "General" tab. Make sure that the Normal option is selected. If not you
may get a message about "selective startup" after you reboot.
◦ Click on "OK"
◦ Next, you must reboot.
◦ After you reboot go to a DOS prompt and type:
path
◦ If c:\jdk1.3\bin appears first your are completed. If c:\windows and/or
c:\windows\command is first, you maybe calling java.exe from those directories.

CLASSPATH variable CLASSPATH -

Goto a DOS prompt Ways to get to a DOS prompt:

• First way: Start, Programs, Command Prompt


• Second way: Start, Run, command.com

DOS Commands
Common DOS Commands

Command Example Description


Steps to Create a directory for this DOS Command. Type a DOS prompt.
project.
c:
md \class
md \class\java
md \class\java\j1
cd \class\java\j1

dir *.* This shows a list of the files in a directory.


C: This takes you to the C: drive.
cd class This changes your default directory to a subdirectory
called class
cd \class\java This changes your default directory to a directory called
"java" which is in a folder called "class" which is in the
root "\" directory of the C: drive.
md \class Makes a directory called class under the root "\" directory.
javac MyFirstApplication.java Compiles your program.
java MyFirstApplication Runs a Java Application
appletviewer MyFirstApplet.html Runs Sun's appletviewer that will display your applet in a
window. No html code is displayed.

www.easydos.com - Helpful info on DOS commands.


Dos Filenames DOS (some OS versions)

• dir /x - shows 8char short file names. Some versions of DOS require short filenames when you
CD or give pathnames. This command is handy in those cases. May not work for all OS's.

Errors Common Errors & Solutions

• Error opening registry key 'Software\JavaSoft\Java Runtime Environment' when calling: java
<program name>
◦ Quick fix - give the full path for java: ex: c:\jdk1.3\bin\java
◦ Reinstall the Java Runtime Environment (JRE)

Potrebbero piacerti anche