• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error: Could not find or load main class one  RSS feed

 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SOMEONE HELP!

Compiled the above and it works just fine. (beginner)
When I run then Iger the: Error: Could not find or load main class one

I have watched countless youtube tutorials as well as the Oracle Documentation on CLASSPATH etc all to no avail, I must be missing something silly little.

Please help, as I am almost losing my mind...
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derek,
Welcome to CodeRanch!
The error implies that it is unable to find the source class file which you are required to run.
did you set the classpath?-this is the directory where it looks for your class file.
what is the Operating system you are using?
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Chimbwanda wrote:. . . Please help, as I am almost losing my mind...


Dereks-MacBook-Air:Lecture1 derekchimbwanda$ javac one.java
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ java one
Error: Could not find or load main class one
Dereks-MacBook-Air:Lecture1 derekchimbwanda$
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please follow the instructions for unix variants-How to set up the classpath.
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much thanks Kumaar,

If I encounter a problem will nag you again>..
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Based on how the code is compiled and ran, meaning no usage of package and the current directory is the same as the location of the source file, it is probably best if you don't use a CLASSPATH.

The default, when not set, is the current directory -- which is what you want here.

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default the classpath is "." which is the current directory.so if you have not done any mischief with the classpath(as you say you have seen many youtube videos) then may be i can help you.check for the file "one.class" in the directory where the source file is located("one.java").if it is there then open the shell and input "cd<space><directory path of the "one.java"> hit the enter then command java one.try it and let us know if it work.
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumaar,

source file and compiled file in the same directory (Lecture1) as below.

However, still getting the error message, I know I must be missing one silly little trick.

Please HELP!

Dereks-MacBook-Air:Desktop derekchimbwanda$ cd javacourse/
Dereks-MacBook-Air:javacourse derekchimbwanda$ cd Lecture1/
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ java one
Error: Could not find or load main class one
Dereks-MacBook-Air:Lecture1 derekchimbwanda
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try declaring your class as public. Right now it's not. Also, class names should be capitalized.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure that declaring the class public will make any difference. I suggest you use the ls instruction and verify where the corresponding .class file is. Also show us what happens when you write
echo $CLASSPATH
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell, hi>

Sorry for late reply different timezone.

Below is feedback coming from my terminal as you suggested I execute commands>


Dereks-MacBook-Air:Desktop derekchimbwanda$ cd javacourse
Dereks-MacBook-Air:javacourse derekchimbwanda$ cd Lecture1/
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ ls one.class
one.class
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ echo $CLASSPATH
:/Users/mian/javaTest
Dereks-MacBook-Air:Lecture1 derekchimbwanda$
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem found!
it is-the classpath is-:/Users/mian/javaTest but it should be "."(default) or some specific directory where you need to put your class file.
for your current session on your command prompt you can set the classpath during running a java class file as:
java -cp <classpath> className
for you it is- java -cp . one,don't forget the dot and the space.but before executing it please cd to the directory Lecture1 as cd javacourse/Lecture1.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this is really annoying as you have to repeatedly mention it each time on cmd so set the classpath permanently as:
export CLASSPATH=.:javacourse/Lecture1 execute it on terminal and please don't forget the dot again.this will set your class path to the current directory and the Lecture1.
then execute your program as java one
try it and let us know about whatever happens.

hope it helps!

Kind Regards,
Praveen.
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumaar,

This is where we are... what am I missing???

Dereks-MacBook-Air:Lecture1 derekchimbwanda$ export CLASSPATH="/Users/derekchimbwanda/Desktop/JavaElements/element.jar"
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ javac one.java
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ java -cp . one
Exception in thread "main" java.lang.NoClassDefFoundError: element/DrawingWindow
at one.main(one.java:6)
Caused by: java.lang.ClassNotFoundException: element.DrawingWindow
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Dereks-MacBook-Air:Lecture1 derekchimbwanda$
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumaar,


Your last message noted, what am I doing wrong???



Last login: Sat Dec 31 11:27:42 on ttys003
Dereks-MacBook-Air:~ derekchimbwanda$ cd Desktop
Dereks-MacBook-Air:Desktop derekchimbwanda$ export CLASSPATH=.:javacourse/Lecture1
Dereks-MacBook-Air:Desktop derekchimbwanda$ echo $CLASSPATH
.:javacourse/Lecture1
Dereks-MacBook-Air:Desktop derekchimbwanda$ cd javacourse/
Dereks-MacBook-Air:javacourse derekchimbwanda$ cd Lecture1/
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ javac one.java
one.java:1: error: package element does not exist
import element.*;
^
one.java:6: error: cannot find symbol
        DrawingWindow d = new DrawingWindow(500,500);
        ^
  symbol:   class DrawingWindow
  location: class one
one.java:6: error: cannot find symbol
        DrawingWindow d = new DrawingWindow(500,500);
                              ^
  symbol:   class DrawingWindow
  location: class one
3 errors
Dereks-MacBook-Air:Lecture1 derekchimbwanda$
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A MILLION Thanks Kumaar!

// Code to Create Drawing Window

import element.*;
class one
{
    public static void main( String [] args)
    {
        DrawingWindow d = new DrawingWindow(500,500);
        d.moveTo(100,100);
        d.lineTo(100,150);
    }
}


// Compiling and running the Program as below after your diligent (Kumaar) guidance

Dereks-MacBook-Air:Lecture1 derekchimbwanda$ export CLASSPATH=".:/Users/derekchimbwanda/Desktop/javacourse/Lecture1/JavaElements/element.jar"
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ echo $CLASSPATH
.:/Users/derekchimbwanda/Desktop/javacourse/Lecture1/JavaElements/element.jar
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ javac one.java
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ java one


// Result:

Drawing Window Created Successfully!!!

 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Code to Create Drawing Window



// Compiling and running the Program as below after your diligent (Kumaar) guidance

Dereks-MacBook-Air:Lecture1 derekchimbwanda$ export CLASSPATH=".:/Users/derekchimbwanda/Desktop/javacourse/Lecture1/JavaElements/element.jar"
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ echo $CLASSPATH
.:/Users/derekchimbwanda/Desktop/javacourse/Lecture1/JavaElements/element.jar
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ javac one.java
Dereks-MacBook-Air:Lecture1 derekchimbwanda$ java one


// Result:

Drawing Window Created Successfully!!!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . set the classpath permanently as:
export CLASSPATH=.:javacourse/Lecture1 . . .
You are right that this is probably a CLASSPATH problem, but setting a permanent CLASSPATH is probably not the right solution. The best solution is probably to find where the CLASSPATH was set in the first place and remove that. You set up a CLASSPATH to access the javaTest directory and now you cannot access anywhere else. Find wherever you set that CLASSPATH (I don't know where that would be on a Mac box, but it would be in ~/.bashrc on a Linux box using bash) and remove the line.

Don't quote the whole of posts; that simply makes the thread longer and longer. If you have :‍D in your posts, click the “Disable smilies” box to prevent it from turing into . I have corrected that for you. The [/code] part of the code tags goes after the code; I have corrected that for you. Why are you using the Desktop folder rather than creating a java directory:-
[open terminal]mkdir java
cd java
You only need the first line once.
 
Derek Chimbwanda
Greenhorn
Posts: 21
Linux Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found where the CLASSPATH was set initially
open ~/.bash_profile
in Mac OS X

Successfully changed that to suit pwd.

And thanks Ritchie for the advice, much appreciated.

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
You really only need a CLASSPATH when you start using external .jars, for example when connecting to a database. Then you will probably need different CLASSPATHs for different applications, so setting a permanent CLASSPATH is more trouble than it is worth.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!