help in java

Status
Not open for further replies.

shivi4

Journeyman
hi i have made a simple java program file

it is compling ok and creating a java class file

but when running it is giving error java.lang.noclassdeffound

exception in thread main normal termination

this is program
class Shivi1
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
System.out.println("i="+i);


}
}
 

enoonmai

Cyborg Agent
Set the CLASSPATH environment variable in Windows by right clicking My Computer>Advanced>Environment Variables>System Variables>New. This should point to the directory in which your .CLASS files are stored. Or run the command this way:

java -classpath <.CLASS directory> Shivi1

If you're using an IDE, select the Preferences options for the tool and add the CLASSPATH info into it too.
 

enoonmai

Cyborg Agent
For God's sakes, could you be a bit more specific. Which directory did you install Java in, what environment variables have you set, where is your .java file, where is your .class file, which IDE (if any) you are using, how are you running your program, etc.

Let me quote an example, lets say I copy your program to the D:\Java\jdk\bin directory and call it Shivi1.java
I compile the program with javac Shivi1.java and it generates the Shivi1.class file, which you run with the
java Shivi1
command.
If it generates a NoClassDefFound error, type the command:
set CLASSPATH=D:\Java\jdk\bin
or whichever directory you've got your class file in (i.e. replace "D:\Java\jdk\bin" with wherever you have stored the .class file) and then re-run the "java Shivi1" command and tell me what happens. Please post more info if we are to help.
 

icecoolz

Cyborg Agent
HAHAHAHA! not working....thats a nice answer. Perhaps I should just reply 'it should work" :p come on dude. Like enoonmai said give us more information will ya!

enoo, His class does compile which means he does have JRE/JDk in his path information. I think the problem is how he is executing the program. Typical error because he is not in the right drectory. Or hasnt given the full package in the command.
 
OP
S

shivi4

Journeyman
thank it work by giving the classpath

i am using editplus 2 can u tell how to configure that out
it is compling normally but giving same type of error
 

enoonmai

Cyborg Agent
@icecoolz: I wasn't talking about the PATH variable pointing to the JDK installation, but was referring to the CLASSPATH environment variable. True, if he's set the PATH to JDK, then it obviously will compile the .java file, but unless he explicitly specifies the CLASSPATH, then it wont execute from the command line, even if its in the same directory as the .java file. Take a look at this screenshot, I removed the CLASSPATH variable from my ENV_VARS list for this example:
*img215.exs.cx/img215/7291/screenshot0lx.th.jpg

I copied the .java file and compiled it, but when I executed it without the CLASSPATH variable, it refused to run, even when it was the same directory, as confirmed by the DIR command. But the second I set the CLASSPATH, it works without a hitch. Now, I personally never use the command line, everything I do is via the Eclipse IDE, which automatically adds the -cp switch corresponding to my workspace, but I guess this is how a newbie would compile/run a Java program. :D Its easier to use the -cp switch for the java.exe file or to maintain a consistent directory for all the .class files rather than creating all files helter-skelter, but I guess the newbies just dont learn all this stuff. Hmm, maybe we need to point them to a good book. :D So, if our friend were to just set the CLASSPATH and stick to a single directory for his .class files, he wouldn't have had this problem. Which is why I push every newbie I find towards the Gel IDE first then towards Eclipse. :lol:

@shivi4: Please take note of our discussion. It might help you as a Java programmer. Also know that while DOS is not case-sensitive, the Java interpreter IS. So
java Shivi1
is not the same as
java shivi1
Make sure you type the exact punctuation as specified in the class name in the source code. Also, its a hard and fast rule among us programmers that you NEVER EVER start a class declaration with a capital letter, it can be only used to separate two words. There's a really long Do's-and-don'ts when it comes to naming variables, classes, objects, methods, etc. but the simple rule is that never start your classname with an uppercase letter, if you want to merge two words together, like Time and Calculation, then you should a classname like timeCalculation. These are the standard naming conventions and should be strictly adhered to if you want to maintain code readability.

Also, I dont know about EditPlus2, but the way you configure ANY IDE is to use the concept of a workspace/project. Create a new one and any self-respecting IDE will automatically use the -cp/-classpath switch to properly compile and run your files. Plus, you maintain a neat, ordered coding workbench. :D

You can set the CLASSPATH variable by following the steps I outlined earlier in my message, by right clicking My Computer>Properties>Advanced>Environment Variables>System Variables and then clicking New and then entering the variable name and its value, which is the directory in which you have the .class files. This works on Windows 2000/XP/2003 systems. If you have any other operating system, click Start>Run and type in msconfig and press Enter. Open the Autoexec.bat tab and click the New button and enter
set CLASSPATH=<directory of your .class files>
for example
set CLASSPATH="G:\My Documents\My Code\Java\Classes"
and then confirm it, hit Apply and OK and you should never see these problems again.
As for IDEs, I personally recommend Gel for a beginner. You can download the small, 4.3MB download from here:
*memescape.co.uk/gexperts/download.html

I highly recommend Gel from personal experience, and once you use it, you will never go back to anything else, except move to something better called Eclipse. :D
 

karthik_rcs

Broken In
Configuring Editplus

Hello shiv,
Steps to configure editplus.
Before that let us assume few things.
Java directory - c:\j2sdk1.4.2
set a variable named PATH -> c:\j2sdk1.4.2\bin

Now
open editplus. goto Tools->preferences,
In the preferences window goto Usertools in the left tree structure.
It must be under tools
Under tools you have User tools, keyboard,spell checker,toolbar.

click the usertools.
now click add tool in the right

1. Menu text - you can give anything - ex. compile java
2. Command - c:\j2sdk1.4.2\bin\javac.exe
Argument - $(FilePath)
Initial directory $(FileDir)
Choose the capture output
-----------

Again
add new tool for execution of java program
command D:\j2sdk1.4.2\bin\java.exe
Argument
$(FileNameNoExt)

Initial dir $(FileDir)

now dont choose capture output...
save and
assign key else ctrl + 1 is compile
ctrl + 2
execute

njoi
 
Status
Not open for further replies.
Top Bottom