Results 1 to 11 of 11
  1. #1
    Kamiama is offline Member
    Join Date
    Feb 2017
    Posts
    5
    Rep Power
    0

    Default Java Error: Could not find or load main class

    I am quite new to java programming. I am following this tutorial to make a simple screen, but my code isn't working out:



    Whenever I try to run the program, the code won't compile. I get an error in the console reading: "Error: Could not find or load main class Lime". I am also getting a syntax error at the bottom of the code reading, "Syntax error on token "Invalid Character", delete this token.

    Lime:

    Java Code:
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class Lime extends JFrame {
        public static void main(String[] args){
    
            DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN);
            Lime b = new Lime();
            b.run(dm);
    
        }
        public void run(DisplayMode dm){
            getContentPane().setBackground(Color.PINK);
            setForeground(Color.WHITE);
            setFont(new Font("Arial", Font.PLAIN, 24));
    
            Screen s = new Screen();
            try{
                s.setFullScreen(dm, this);
                try{
                    Thread.sleep(5000);
                }catch(Exception ex){}
            }finally{
                s.restoreScreen();
            }
        }
        public void paint(Graphics g){
            super.paint(g); 
            g.drawString("this is awesome",300, 500);
        }
    }
    Screen:

    Java Code:
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class Screen {  
        private GraphicsDevice vc; 
        public Screen(){
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            vc = env.getDefaultScreenDevice();
        }
        public void setFullScreen(DisplayMode dm, JFrame window){
            window.setUndecorated(true);
            window.setResizable(false);
            vc.setFullScreenWindow(window);
    
            if(dm != null && vc.isDisplayChangeSupported()){
                try{
                    vc.setDisplayMode(dm);
                }catch(Exception ex){}
            }
        }
        public Window getFullScreenWindow(){
            return vc.getFullScreenWindow();
        }
        public void restoreScreen(){
            Window w = vc.getFullScreenWindow();
            if (w != null){
                w.dispose();
            }
            vc.setFullScreenWindow(null);
    
    
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: Java Error: Could not find or load main class

    I get an error in the console reading: "Error: Could not find or load main class Lime".
    Is there a Lime.class file in the folder where you issue the java command?
    Can you copy the full text of the command prompt window from when you try to execute the program and paste it here?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Kamiama is offline Member
    Join Date
    Feb 2017
    Posts
    5
    Rep Power
    0

    Default Re: Java Error: Could not find or load main class

    Thanks for the response! I looked in my parent folder and there doesn't appear to be a Lime.class file. There is only a screen.class file. I don't get what you mean by the command prompt. If you mean the console, then the only output is:

    Error: Could not find or load main class Lime

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: Java Error: Could not find or load main class

    Please copy the FULL CONTENTS of the console and paste it here so we can see the command that you entered and what folder it is in.
    Also enter the dir command to show what files are in that folder where you enter the java command.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kamiama is offline Member
    Join Date
    Feb 2017
    Posts
    5
    Rep Power
    0

    Default Re: Java Error: Could not find or load main class

    I use eclipse for my testing:

    Java Error: Could not find or load main class-capture.png

    The directory of my folder is: C:\JavaProject\Kamon. Lime.java and Screen.java are the only two files in the parent directory.

    (I will also run from my Command Panel and get some screenshots)

    EDIT:

    Here is the default console:

    Java Error: Could not find or load main class-capture.png

    EDIT:

    I tried to run it via Eclipse again and I got this error message:

    Java Error: Could not find or load main class-capture.png
    Last edited by Kamiama; 02-25-2017 at 11:16 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,964
    Rep Power
    31

    Default Re: Java Error: Could not find or load main class

    The console contents shows that The OS can't find the javac command. The JDK's bin folder needs to be in the PATH variable so the OS can find the javac.exe file.

    Take a look at the tutorial:
    "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    Lesson: Common Problems (and Their Solutions) (The Java™ Tutorials > Getting Started)
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Java Error: Could not find or load main class

    Remove the dot that you have in line 33 of your code in the Lime class. Tell us whether that resolves the saving issue.

  8. #8
    Kamiama is offline Member
    Join Date
    Feb 2017
    Posts
    5
    Rep Power
    0

    Default Re: Java Error: Could not find or load main class

    @Norm Well, I have run other programs before, it just seems to be this program that is messing up. I'll see what I can do though.

    @benji2505 I have tried to remove the syntax error, but can't figure out how. This is what it reads, "Syntax error on token 'Invalid Character', delete this token"

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Java Error: Could not find or load main class

    imho you have a character in line 33 of that file (outside the class). Erase it.

  10. #10
    Kamiama is offline Member
    Join Date
    Feb 2017
    Posts
    5
    Rep Power
    0

    Default Re: Java Error: Could not find or load main class

    I found it. If you look really closely at the 33rd line:

    Java Error: Could not find or load main class-capture.png

    You can see a pesky little period. That is what was causing the problem, and in looking through my code for the syntax error dozens of times, it just hid from me. Thanks for the help.
    Last edited by Kamiama; 02-26-2017 at 01:24 AM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Java Error: Could not find or load main class

    The key thing here is that there is no point trying to execute code that doesn't even compile.
    You cannot run code that has those red x's in it...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 41
    Last Post: 05-28-2016, 12:40 PM
  2. Replies: 2
    Last Post: 10-29-2013, 08:00 PM
  3. Replies: 7
    Last Post: 01-01-2012, 12:29 PM
  4. Replies: 3
    Last Post: 12-09-2011, 03:51 AM
  5. Error: Could not find or load main class Ops.java
    By Javasubbu in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 10:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •