• 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

running program in own folder?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have encountered a problem with my program named Break1.

I compliled it in my folder named jyo. Break1.class file is now available there in jyo folder.

but when I try to run it I get the following error.

C:\jyo>java Break1
Exception in thread "main" java.lang.NoClassDefFoundError: Break1
Caused by: java.lang.ClassNotFoundException: Break1
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
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: Break1. Program will exit.

When I run it in the original bin folder it gets the output.

Why class file been not found???
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you have a package name in your .java file? That means you can only execute the application from outside the folder the .class file is in.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Did you have a package name in your .java file?



No, I dont have any package inside my .java file.

Campbell Ritchie wrote:That means you can only execute the application from outside the folder the .class file is in.



and couldnt understand the above .
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have no package declarations, you can only execute the .class files from the directory they are in. Any other .class files must be in corresponding directories, depending on their package structure. If they have no package names, they must be in the same directory.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have my .class & .java files in C:\jyo.
both are in the same folder same directory.

Compiled with the same directory.

But could not execute the class from the same path??
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you could show the code?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is in your system CLASSPATH? That may be the problem.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kalabaw moo wrote:Maybe you could show the code?


Campbell Ritchie wrote:What is in your system CLASSPATH? That may be the problem.


class Break1
{
public static void main(String ar[])
{
int i=1;

while(i<100)
{
if(i==10)
break;
System.out.println("i="+i);
i++;
}
}
}


I have my JDK installed in C:\Program Files\Java folder.

And above java file & its class file in the C:\jyo folder.

When wanted to compile & execute the code directly from the c:jyo in command prompt.
I did
C:\jyo>set path=C:\Program Files\Java\jdk1.6.0_14\bin

I could compile it in c:\jyo directory.

But while executing it shows ClassNotFound exception.

i.e.

C:\jyo>java Break1
Exception in thread "main" java.lang.NoClassDefFoundError: Break1
Caused by: java.lang.ClassNotFoundException: Break1
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
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: Break1. Program will exit.

And the class file is there in the folder C:\jyo.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I myself wrote:What is in your system CLASSPATH? That may be the problem.

Please check; you didn't answer that. Your problem might be caused by an incorrect CLASSPATH. The errors you are seeing suggest you have a correct PATH.
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


put public in your class.. recompile and run
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kalabaw moo wrote:put public in your class.. recompile and run

What difference will that make?

I still think it might be an incorrect system CLASSPATH.
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kalabaw moo wrote:put public in your class.. recompile and run

What difference will that make?

I still think it might be an incorrect system CLASSPATH.




I just saw this link.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both classes are in the same package; they have access to each other whether they are marked public or not. Your JVM has access to the package when you call java practice.TestIOMain, so I don't think making the classes public will make any difference.

Jyoti: please tell us what's in your system CLASSPATH.
 
Kalabaw moo
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, but how come that type of issue happened? and the resolution provided was just putting public on the class. That made me curious.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Both classes are in the same package; they have access to each other whether they are marked public or not. Your JVM has access to the package when you call java practice.TestIOMain, so I don't think making the classes public will make any difference.

Jyoti: please tell us what's in your system CLASSPATH.



public or default doesnt make any difference.

my CLASSPATH is set to "C:\Program Files\Java\jdk1.6.0_14\bin".

If I do put .class file in the bin folder of jdk, I could be able to execute the class from the path "c:\jyo".
Unless .class file is in the bin folder, the class does not execute & gives ClassNotFound exception.

Now, is that neccesary to keep the .class files in the bin folder of jdk to execute it from any other folder ???
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that all you have in your CLASSPATH? If so, that shouldn't be in there in the first place. If you have nothing else in your CLASSPATH, delete the CLASSPATH altogether. If you have anything else in the CLASSPATH, replace the ...java/kdj1.6... entry with .;

If you end up with an empty CLASSPATH and are still getting those exceptions, try changing your system CLASSPATH to . That is one full stop/dot/period. It means "current directory".

Don't put your own work in the bin folder; that is for installed programs. If you install a new version of Java and uninstall the old version, all your work may vanish.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have deleted the system CLASSPATH & its working finely now .

I guess it was checking the .class file in the system CLASSPATH .

Thanku very much for your suggestions .
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done . If you have no system CLASSPATH then it tries the "current directory." Beware: there are a few applications (QuickTime maybe) which set a system CLASSPATH when they are installed. So you might need to edit your CLASSPATH by adding a . if the problem ever recurs.

Oddly enough, there is somebody else on these fora today who may have the same problem.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done . If you have no system CLASSPATH then it tries the "current directory." Beware: there are a few applications (QuickTime maybe) which set a system CLASSPATH when they are installed. So you might need to edit your CLASSPATH by adding a . if the problem ever recurs.



yes definitely! thanku .
 
Campbell Ritchie
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're welcome
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having basically the same problem but I can't get rid of the classpath because I don't have admin rights to the machine. I downloaded a DB and it has a java file that does some configuring. I compiled the .java file fine. The .java and the .class files are in the same directory. I open a CMD prompt window and go to that directory and enter the javac command and get the same error message mentioned previously in the thread. I'm not sure how to fix this.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't "hijack" somebody else's thread by asking a new question.

You get a "java is not recognized . . . " message from the operating system if the PATH is incorrect. You get a ClassDefNotFoundException if your class file is not in the CLASSPATH. The installation instructions (I think ยง4) tell you how to set your PATH, which can be changed from the command line. The documentation for the java.exe tool and the javac.exe tool tell you how to set the CLASSPATH for each invocation. If you cannot find classes in the current directory, you would want

javac -cp . Foo.java
java -cp . Foo

or something simlar.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done . If you have no system CLASSPATH then it tries the "current directory." Beware: there are a few applications (QuickTime maybe) which set a system CLASSPATH when they are installed. So you might need to edit your CLASSPATH by adding a . if the problem ever recurs.

Oddly enough, there is somebody else on these fora today who may have the same problem.




Campbell Ritchie thanks a lot man!!! QuickTime player was installed in my pc and i was unable to figure out the problem.Your post saved my java course
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for delay, but welcome to the Ranch and you’re welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic