aspose file tools*
The moose likes Beginning Java and the fly likes Execution Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Execution Error" Watch "Execution Error" New topic
Author

Execution Error

Jdinesh Tiwari
Ranch Hand

Joined: Jan 13, 2013
Posts: 35
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
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jdinesh Tiwari
Ranch Hand

Joined: Jan 13, 2013
Posts: 35
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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

Joined: Jan 13, 2013
Posts: 35
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


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jdinesh Tiwari
Ranch Hand

Joined: Jan 13, 2013
Posts: 35
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
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execution Error