• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Execution Error

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am facing problem with this code while executing it

public class Light {
// Static variable
static int counter; // Default value 0 when class is loaded.

// Instance variables
int noOfWatts = 100; // Explicitly set to 100.
boolean indicator; // Implicitly set to default value false.
String location; // Implicitly set to default value null.

public static void main(String[] args) {
Light bulb = new Light();
System.out.println("Static variable counter: " + Light.counter);
System.out.println("Instance variable noOfWatts: " + bulb.noOfWatts);
System.out.println("Instance variable indicator: " + bulb.indicator);
System.out.println("Instance variable location: " + bulb.location);
return;
}
}


This program on execution is giving error as: Could not find or load main class Light

May kindly help me what is the problem with this code
Regards,
Dinesh
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code looks fine. Please check if you have saved the java file with the name "Light .java". The error seems to be because of this only.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you compile it?

Did you get a Light.class file?

what EXACTLY did you type to run it?

I ask because i had no problems saving your code, compiling it, and running it as-is. However, if I delete my Light.class file, I get the exact error you list.

Do you have a classpath environment variable set, and if so, set to what?

What directory are you trying to run this from? What directory is your .class file in?
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for looking up into the problem. As per your suggestion the problem was with the path.

When i compiled my program using

javac Light.java It works fine

But when I tried to run the program using
java Light

Then This program on execution is giving error as: Could not find or load main class Light

But when I run this using
java -cp D:/JAVAPROGMS/Light

then it works fine

MAY KINDLY EXPLAIN WHY I HAVE TO SPECIFY THE FULL path LOCATION DURING ITS EXECUTION WHEREAS ITS NOT REQUIRED IN ITS COMPILATION ?

Regds,
Dinesh
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must be working one directory above the directory containing the Light.java file. It is odd that you need the package name; have you omitted the package declaration when you posted that code?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a CLASSPATH variable set? if so, what is it set to?

Note: I am not suggesting that you SHOULD have one set. Often, for beginners, it is better to NOT have it, but if you do it could be causing issues.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your kind responses

Fred Sir

The classpath for java has been set as
C:\Program Files\Java\jdk1.6.0\bin;

C:\Program Files\Java\jdk1.6.0 -- > This causes the issue if i dont`t put semicolon at the end, than i am unable to find my java environment on command prompt by simply giving the command
javac




and Ritche Sir,
I haven`t omitted the package name of my program.
Actually I haven`t kept it in any package nor even in default.
As being new to java, for now i am not using any of the IDs. I am working with NotePad++.
The program is as it is I checked it again.
Its executing for now. Not only this program other basic programs are also executing successfully but the issue is just with the run time
where i have to specify the full path of the location using :
java -cp D:/JAVAPROGAM Light
rather than simple command
java Light

Regards
Dinesh


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jdinesh Tiwari wrote:The classpath for java has been set as
C:\Program Files\Java\jdk1.6.0\bin;


That's wrong. The bin directory of the JDK does not normally contain Java class files. You should not put it in the classpath. (Note: You should add the bin directory of the JDK to the PATH, but not to the CLASSPATH).

Actually, it's better to not set the CLASSPATH environment variable at all. If it is not set, Java will by default use the current directory "." as the classpath.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jong thanks for your kind suggestions

The Environment variables has been set are as follows:

Under User Variable :

[b]CLASSPATH
C:\apache-tomcat-7.0.27\lib\servlet-api.jar

JAVA_HOME
C:\Program Files\Java\jdk1.6.0\bin

JRE_HOME
C:\Program Files\Java\jdk1.6.0

path
C:\Program Files\Java\jdk1.6.0\bin;


Still the problem existing :-

I m able to compile my program using
javac Light.java

But unable to run my program using
java Light

Instead I have to give the command as
java -cp D:/JAVAPROGM Light

to execute the same.

Why? May help on this
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the CLASSPATH variable tells the java.exe where to look for class files. your classpath is set to this:

CLASSPATH
C:\apache-tomcat-7.0.27\lib\servlet-api.jar


So, when you type

java Light

Java looks in the C:\apache-tomcat-7.0.27\lib\servlet-api.jar directory, can't find your .class file, and reports as such. When you say

java -cp D:/JAVAPROGM Light

you are explicitly saying to it "Look in the D:/JAVAPROGM directory" (i'm not sure if it still looks in the CLASSPATH dirs as well).

So, you have a couple options.

1) ADD the "D:/JAVAPROGM" directory to your classpath.
2) cd into the D:/JAVAPROGM directory before you run "java Light" - although you probably still need to add the dot to your CLASSPATH, saying "use the current directory".
3) get rid of the CLASSPATH all together (although this could break other things) and CD to your D:/JAVAPROGM directory.

I think that's all correct...
reply
    Bookmark Topic Watch Topic
  • New Topic