Cannot compile Java application - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Cannot compile Java application

zandgreenzandgreen Posts: 1Member
Hello, I am new to Java and programming in general. I have a source of an application written in Java that I want to change a part of and compile it. Here is the file I am working on:
[code]package sarf.ui;

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.JOptionPane;

/**
*

Title:

*
*

Description:

*
*

Copyright: Copyright (c) 2006

*
*

Company:

*
* @author not attributable
* @version 1.0
*/
public class MainApp {
boolean packFrame = false;

/**
* Construct and show the application.
*/
public MainApp() {

// Center the window

Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}

/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
//MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.OceanTheme());
UIManager.setLookAndFeel(MetalLookAndFeel.class.getName());
}
catch (Exception exception) {
exception.printStackTrace();
}

new MainApp();
}
});
}
}

[/code]

When I try to compile it, I get the following error:
[code][zand@zand ui]$ /opt/java/bin/javac -verbose MainApp.java
[parsing started MainApp.java]
[parsing completed 39ms]
[search path for source files: .]
[search path for class files: /opt/java/jre/lib/resources.jar,/opt/java/jre/lib/rt.jar,/opt/java/jre/lib/sunrsasign.jar,/opt/java/jre/lib/jsse.jar,/opt/java/jre/lib/jce.jar,/opt/java/jre/lib/charsets.jar,/opt/java/jre/classes,/opt/java/jre/lib/ext/dnsns.jar,/opt/java/jre/lib/ext/localedata.jar,/opt/java/jre/lib/ext/sunjce_provider.jar,/opt/java/jre/lib/ext/sunpkcs11.jar,.]
[loading java/awt/Toolkit.class(java/awt:Toolkit.class)]
[loading javax/swing/SwingUtilities.class(javax/swing:SwingUtilities.class)]
[loading javax/swing/UIManager.class(javax/swing:UIManager.class)]
[loading java/awt/Dimension.class(java/awt:Dimension.class)]
[loading javax/swing/plaf/metal/MetalLookAndFeel.class(javax/swing/plaf/metal:MetalLookAndFeel.class)]
[loading javax/swing/JOptionPane.class(javax/swing:JOptionPane.class)]
[loading java/lang/Object.class(java/lang:Object.class)]
[loading java/lang/String.class(java/lang:String.class)]
[checking sarf.ui.MainApp]
MainApp.java:32: cannot find symbol
symbol : variable frame
location: class sarf.ui.MainApp
Dimension frameSize = frame.getSize();
^
MainApp.java:33: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
if (frameSize.height > screenSize.height) {
^
MainApp.java:34: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
frameSize.height = screenSize.height;
^
MainApp.java:36: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
if (frameSize.width > screenSize.width) {
^
MainApp.java:37: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
frameSize.width = screenSize.width;
^
MainApp.java:39: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
^
MainApp.java:39: cannot find symbol
symbol : variable screenSize
location: class sarf.ui.MainApp
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
^
MainApp.java:39: cannot find symbol
symbol : variable frame
location: class sarf.ui.MainApp
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
^
MainApp.java:40: cannot find symbol
symbol : variable frame
location: class sarf.ui.MainApp
frame.setVisible(true);
^
[loading java/lang/Runnable.class(java/lang:Runnable.class)]
[loading java/lang/Class.class(java/lang:Class.class)]
[loading javax/swing/LookAndFeel.class(javax/swing:LookAndFeel.class)]
[loading java/lang/Exception.class(java/lang:Exception.class)]
[loading java/lang/Throwable.class(java/lang:Throwable.class)]
[total 349ms]
9 errors[/code]

As far as I understand I am missing some standard class library to work .getSize though it seems to load some classes from /opt/java/jre/lib/.

Comments

  • Josh CodeJosh Code Posts: 675Member
    Your file is in a package named sarf.ui and this will affect the kind of compiling command you have to use and where to store your file.

    Make a file directory structure like this for your code:
    sarf
    - ui
    - - MainApp.java

    In other words, make directory sarf. make a directory ui to go within sarf. make file MainApp.java to go within ui.

    Your MainApp.java had some undeclared variables like frame and screensize so it was updated to work. Here is the contents of MainApp.java:
    [code]
    package sarf.ui;

    import java.awt.Toolkit;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import java.awt.Dimension;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;

    /**
    *

    Title:

    *
    *

    Description:

    *
    *

    Copyright: Copyright (c) 2006

    *
    *

    Company:

    *
    * @author not attributable
    * @version 1.0
    */
    public class MainApp {
    boolean packFrame = false;
    JFrame frame;

    /**
    * Construct and show the application.
    */
    public MainApp() {
    // create the frame.
    frame = new JFrame("The Window");
    frame.setSize(400,400);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();

    // Center the window


    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
    }

    /**
    * Application entry point.
    *
    * @param args String[]
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    try {
    //MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.OceanTheme());
    UIManager.setLookAndFeel(MetalLookAndFeel.class.getName());
    }
    catch (Exception exception) {
    exception.printStackTrace();
    }

    new MainApp();
    }
    });
    }
    }
    [/code]

    To compile that from command line, go to the directory above sarf, the root directory of your code. From there, use this command:
    [code]
    javac sarf/ui/*.java
    [/code]

    To run the resulting .class file, execute from the same directory as compilation, the following command:
    [code]
    java sarf.ui.MainApp
    [/code]

    Without the line "package sarf.ui;" in your code, there would be no need to worry about the directory structure.
Sign In or Register to comment.