How to: Create a Visual J# Console Application (Visual J# Express) 

The purpose of this topic is to acquaint you with the Visual J# Express Edition development environment as you build the simplest form of a Visual J# program, a console application. Console applications perform all their input and output at the command line, so they are ideal for quickly testing language features, doing simple exercises, and writing command line utilities.

Note

The features of the development environment discussed in this section will also be encountered when developing Windows Forms applications, so don't skip this part simply because you don't plan on writing console applications.

In this section, you'll learn:

  • How to create a new console application.

  • How to view Solution Explorer.

  • How to keep your code nicely formatted.

  • How to use IntelliSense to make entering code faster and more accurate.

  • How to build and run your application.

The program you create in this task makes use of the System.out class to obtain and display a list of all the files, and their sizes, contained in a directory. You could use this code as a basis for a utility that searches a directory for a particular file name.

The following example shows you how to create a simple console application called List Files that displays information about files from a particular directory.

To create a console application

  1. On the File menu, click New, and then click Project.

    The New Project dialog box appears. This dialog box lists the different project types that Visual J# Express can create.

  2. Select the Console Application template, and change the name of your application to List Files.

  3. Click OK.

    Visual J# Express creates your project named after the project title, and opens the main Visual J# Express window, including the code pane, where you will enter and edit the J# source code that makes up your application. The project is stored in a temporary location until you actually save it.

    VJS Express List Files

    Notice the toolbar at the top of the window. This toolbar contains icons for creating, loading and saving projects, editing your source code, building your application, and hiding and displaying other windows that make up the Visual J# Express environment. The four icons at the far right of this toolbar are used to open important windows such as Solution Explorer and the Toolbox. Point to any of these icons to get pop-up ToolTip Help.

  4. Click on the Solution Explorer tab on the right of the screen, or the Solution Explorer icon in the toolbar.

    Solution Explorer is a very useful pane, as it displays the various files that make up your project. The most important file in this project is the file Program.jsl, which contains the source code for your application.

    VJS Express List Files Solution Explorer

  5. Now click to the right of the open curly brace ({) inside the main method, and press ENTER to start a new line.

    Notice how the editor automatically indents the cursor.

  6. Type the class name System. into the Code Editor.

    When typing a J# class name or keyword, you have a choice: you can either type the complete word yourself, or let the IntelliSense tool that is part of the Code Editor do it for you. For example, when you type sys, a pop-up list of words is displayed as IntelliSense attempts to predict the word you are typing. In this case, you won't see the word "System" displayed just yet, so either scroll down the list, or continue typing the word "system." When "system" is highlighted in the list, press ENTER, or TAB, or double-click it, and System will be added to your code.

    VJS Express List Files Intellisense

    The advantage of using IntelliSense is that you can be certain the casing and the spelling are correct. Whether you type the code or let IntelliSense do it is entirely up to you.

  7. Type a period and the class name out, followed by another period.

    As soon as you type the period after out, another IntelliSense list is displayed. This list contains all the possible classes, methods, and properties that are part of the out stream class. You want the println method, and you should be able to see it at the bottom of the list. Either finish typing println yourself, or press the DOWN ARROW key until it is selected, and press ENTER, or TAB, or double-click it. println will be added to your code.

    VJS Express List Files Intellisense 2

  8. Type an open parenthesis ((). You'll immediately see another IntelliSense feature—the method signatures appear as a tool tip message. In this case you can see there are 10 different signatures, and you can look through them by clicking the UP and DOWN ARROW keys.

  9. Type the string "This program lists all the files in the directory."

    Type the message inside quotation marks, and add a closing parenthesis ()). You'll see a red wavy underline displayed as a reminder that something is missing. Type a semicolon (;) and the wavy underline will disappear.

  10. Finish the program.

    Type or copy and paste the following code to complete the program:

    import java.io.File;
    
    public class Program
    {
        public static void main(String[] args)
        {
            System.out.println("This program lists all the files in the directory:");
    
            File filePath = new File("c:\\Program Files");
            java.io.File[] fileList = filePath.listFiles();
            for (int idx = 0; idx < fileList.length; idx++)
            {
                System.out.println(fileList[idx]);
            }
        }
    }
    
  11. Run your program.

    Your first program is now complete and ready to compile and run. To do this, either press F5 or click on the Start icon in the toolbar.

    VJS Express List Files Start

    Once the program compiles and runs, the Console window opens and a list of files and their sizes is displayed. Press ENTER to exit the program.

    If you are new to J# programming, this would be a good time to read the Visual J# Referencesection, and try out some of the language examples. If you want to know more about the Visual J# Express development environment, and how to create Windows Applications, move on to the next section, How to: Create a Visual J# Windows Application

See Also

Other Resources

Using Visual J# Express
Visual J# Express Development Environment Features
Visual J# Express Tips and Tricks
Visual J# Reference