Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Why wont this compile in Visual J++ 1.1 ??

Posted on 1998-05-04
5
296 Views
Last Modified: 2013-11-23
I am trying to compile this program from straight out of a book..
import java.awt.*;
import java.awt.event.*;

public class HelloWindowsApp extends Frame {
 public static void main(String args[]){
  HelloWindowsApp app = new HelloWindowsApp();
 }
 
 public HelloWindowsApp() {
  super("Toby Rules the World!");
  setSize(200,200);
  addWindowListener(new HelloWindowsApp.WindowEventHandler());
  show();
 }
 
 public void paint(Graphics g) {
  g.drawString("Hello Windows!",50,90);
 }
 
 class WindowEventHandler extends WindowAdapter {
  public void windowClosing(WindowEvent e){
   System.exit(0);
  }
 }
}

Very simple nothing to it really but when I try to compile in the trial version of Visuall J++ 1.1 I get the errors:
--------------------Configuration: HelloWindowsApp - Java Virtual Machine Debug--------------------
Compiling...
Microsoft (R) Visual J++ Compiler Version 1.01.7022
Copyright (C) Microsoft Corp 1996. All rights reserved.
D:\My Programs\My Own tests\HelloWindowsApp.java(20,2) : error J0021: Expected type specifier
D:\My Programs\My Own tests\HelloWindowsApp.java(20,2) : error J0019: Expected identifier
D:\My Programs\My Own tests\HelloWindowsApp.java(23,3) : fatal error J0020: Expected 'class' or 'interface'
Error executing jvc.exe.

HelloWindowsApp.class - 3 error(s), 0 warning(s)

the strange thing is this program will compile fine using the dos compiler: javac.
i.e javac HelloWindowsApp.java
no errors, so whats the problem???

0
Comment
Question by:deeznutz
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220544
Looks like you have misplaced brackets. Try:

import java.awt.*;
import java.awt.event.*;

public class HelloWindowsApp extends Frame {
     public static void main(String args[]){
        HelloWindowsApp app = new HelloWindowsApp();
     }
     
     public HelloWindowsApp() {
        super("Toby Rules the World!");
        setSize(200,200);
        addWindowListener(new HelloWindowsApp.WindowEventHandler());
        show();
      }
     
      public void paint(Graphics g) {
         g.drawString("Hello Windows!",50,90);
      }
 }
class WindowEventHandler extends WindowAdapter {
      public void windowClosing(WindowEvent e){
         System.exit(0);
      }
}

The problem, from looking at the code, is that the missplaced bracket causes the compiler to try to read class WindowEventHandler as a method of HelloWindowsApp instead of as an independent class.

Note that this will compile, but will not run. In order for it to run, you will need to rewrite it as:

import java.awt.*;
    import java.awt.event.*;

    public class HelloWindowsApp extends Frame {
     public static void main(String args[]){
      HelloWindowsApp app = new HelloWindowsApp();
     }
     
     public HelloWindowsApp() {
      super("Toby Rules the World!");
      setSize(200,200);
      addWindowListener(new HelloWindowsApp.WindowEventHandler());
      show();
     }
     
     public void paint(Graphics g) {
      g.drawString("Hello Windows!",50,90);
     }
     
     void WindowEventHandler extends WindowAdapter {
      public void windowClosing(WindowEvent e){
       System.exit(0);
      }
     }
    }

Since event handlers are overriden methods of the object class.
0
 
LVL 1

Author Comment

by:deeznutz
ID: 1220545
Have you tried compiling both your code in Visual J++ 1.1?
I get exactly the saem error, what I cannt understand which you may have missed is that, my first code comiles fine using
the dos command javac HelloWindowsApp.java, but will not compile in J++.

0
 
LVL 5

Expert Comment

by:fontaine
ID: 1220546
As I am not sure, I post as a comment. Tell me if this is an answer. The problem is the inner class. This is pure JDK 1.1 and I think that VJ++ does not support it. Take a look at the doc. about this (I don't have VJ++ by hand). If you put the inner class outside of HelloWindowsApp and turn new HelloWindowsApp.WindowEventHandler() in new WindowEventHandler(), the program should compile without any problem.
0
 
LVL 1

Author Comment

by:deeznutz
ID: 1220547
Thanks, but you have added a comment, and I cannt give you the credit for your answer.  You answer was spot on, so if you want you deserve the points for this question, but I am unable to grade comments. :(
0
 
LVL 5

Accepted Solution

by:
fontaine earned 100 total points
ID: 1220548
Thanks, deeznutz! I repost as an answer then.

The problem is the inner class. This is pure JDK 1.1 and VJ++ does not support it. If you put the inner class outside of HelloWindowsApp and turn new HelloWindowsApp.WindowEventHandler() in new WindowEventHandler(), the program should compile without any problem.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
web project error add remove 1 29
Java 8 to Java 6 8 21
jsp error 6 26
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question