Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassNotFoundException error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassNotFoundException error" Watch "ClassNotFoundException error" New topic
Author

ClassNotFoundException error

Divya Sanjeev
Greenhorn

Joined: Dec 16, 2009
Posts: 18
Hi,

Following is the piece of code which I tried but I am getting ClassNotFoundException. Please help.

package scjp;

import java.io.File;

public class FileDemo1 {

public static String getFileName(String fileName)throws Exception{
if(fileName!=null || fileName.length()!=0){
if(fileName.indexOf(File.separator) != -1){
if(!(new File(fileName)).isDirectory()){
fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1,fileName.length());
if(fileName.indexOf('.')!=-1){
return fileName.substring(0,fileName.indexOf('.'));
}else{
return fileName;
}
}
else{
throw new Exception("The File name is Directory.");
}
}
else
{
if(fileName.indexOf('.') != -1){
return fileName.substring(0,fileName.lastIndexOf('.'));
}else{
return fileName;
}
}
}
else{
throw new Exception("The File name Can not be empty.");
}
}

public static void main(String[] args) throws Exception{
String s = "C:/scjp/examples/Filedemo.java";
FileDemo1.getFileName(s);
}
}
Ramesh Kumar Muthukumar
Greenhorn

Joined: Oct 22, 2007
Posts: 8

What is the name of the java file that you have kept?
Ramesh Kumar Muthukumar
Greenhorn

Joined: Oct 22, 2007
Posts: 8

the same code works for me without any exception.
Divya Sanjeev
Greenhorn

Joined: Dec 16, 2009
Posts: 18
I have named the file as FileDemo1.java
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Post your full error.

It will help to identify your problem


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Divya Sanjeev
Greenhorn

Joined: Dec 16, 2009
Posts: 18
This was the error I got when I tried to run the program.


D:\jxlfiles\scjp>java FileDemo1
Exception in thread "main" java.lang.NoClassDefFoundError: FileDemo1 (wrong name: scjp/FileDemo1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: FileDemo1. Program will exit.
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
Hi Divya,

I have analysed your code and found that all you want "to extract the name of file without '.' extension from system file path parsed".
correct me if I am wrong.........
I have run your code in my system but I am not getting any exception suggesting most possibly file doesn't exist.But it is not making things better for you further because I am not getting any output also...
Your code seems to have some structural as well as logical defects.

1.Why are you putting so many nested conditions.Try to put them in a single or two.

2."file.separator" would be loaded as a system property, for windows operating system it would be a backward slash, '\'.
i know for escaping "illegal escape character exception" you are using forward slash '/',but it would not solve your purpose any more.
You should use '\\' for the same.

I tried to modify your code and end up with code snippet given below.It may serve your purpose.
Also check the path you are providing exists or not.... .Try it-

Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105

I check your code.

Same error occur me. when i run this code, without compile the code.

here i give my command prompt output here :

First i check dir command :





I hope its help for you
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
Divya,Have you compiled the source code before running the proggram
?...........
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

Divya,
you should respect the fact that FileDemo1 class lives inside the scjp package. So
0) Optionally, put the current directory into your class path
1) go to one directory above
2) javac scjp/FileDemo1.java (to compile)
3) java -cp "." scjp.FileDemo1 (to execute)


OCPJP 6.0
Divya Sanjeev
Greenhorn

Joined: Dec 16, 2009
Posts: 18
Thanks Achilleas.

That was the mistake which I had done. I had left scjp package from the path.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException error