• 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

ClassNotFoundException error

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the name of the java file that you have kept?
 
Ramesh Kumar Muthukumar
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the same code works for me without any exception.
 
Divya Sanjeev
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have named the file as FileDemo1.java
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your full error.

It will help to identify your problem
 
Divya Sanjeev
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divya,Have you compiled the source code before running the proggram
?...........
 
Ranch Hand
Posts: 125
Postgres Database BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Divya Sanjeev
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Achilleas.

That was the mistake which I had done. I had left scjp package from the path.
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic