wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes running program in own folder? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "running program in own folder?" Watch "running program in own folder?" New topic
Author

running program in own folder?

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
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???


thanks
Jyo
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: Jun 30, 2009
Posts: 142
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: Jun 30, 2009
Posts: 142

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??
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Maybe you could show the code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
What is in your system CLASSPATH? That may be the problem.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: May 26, 2009
Posts: 61


put public in your class.. recompile and run
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: May 26, 2009
Posts: 61
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: May 26, 2009
Posts: 61
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

Joined: Jun 30, 2009
Posts: 142
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: Jun 30, 2009
Posts: 142


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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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

Joined: Jun 30, 2009
Posts: 142
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
You're welcome
R Jarman
Greenhorn

Joined: Feb 08, 2005
Posts: 27
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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.
rishi br
Greenhorn

Joined: Dec 28, 2012
Posts: 1
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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Sorry for delay, but welcome to the Ranch and you’re welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running program in own folder?