Results 1 to 7 of 7
Thread: classpath - packaging problem
- 07-30-2011, 07:13 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
classpath - packaging problem
Hi all, I spent all day today trying to figure this out but no luck so far, maybe a keen eye can see better than me.
I am learning about packagin and I have a program that I am trying to run and I get the infamous error COULD NOT FIND THE MAIN CLASS
this is the code :
package ch4.access;
class WithProtected {
protected int i;
}
public class E06_ProtectedManipulation {
public static void main(String args[]) {
WithProtected wp = new WithProtected();
wp.i = 47;
System.out.println("wp.i = " + wp.i);
}
}
and this is the error :
d:\JavaProjects\TIJ4\ch4\access>javac E06_ProtectedManipulation.java
d:\JavaProjects\TIJ4\ch4\access>java E06_ProtectedManipulation
Exception in thread "main" java.lang.NoClassDefFoundError: E06_ProtectedManipu
tion (wrong name: ch4/access/E06_ProtectedManipulation)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: E06_ProtectedManipulation. Program will exit.
PS: the file is in here D:\JavaProjects\TIJ4\ch4\access\E06_ProtectedManip ulation.java
CLASSPATH is set to :
.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip; d:\JavaProjects\TIJ4
Running Windows 7 Professional 64 bit
Any help would be greatly appreciated
- 07-30-2011, 07:38 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I think the runtime is complaining because you have asked to the class E06_ProtectedManipulation but there is no such class. It is suggesting that perhaps you mean the class ch4.access.E06_ProtectedManipulation because it can find that class.wrong name: ch4/access/E06_ProtectedManipulation
Try the following:
The dash-cp-space-dot arguments are setting the classpath to be used (by both the compiler and the runtime). There is some latitude about the exact the exact commands to use, but using these two from the "top" of the desired classpath is common and straightforward. javac wants as an argument the file containing the Java source code, and that can be specified however your OS allows you to specify files at the command line. java, on the other hand, wants the name of a class which contains the main method, and that means the full name packages and all.Java Code:d:\JavaProjects\TIJ4>javac -cp . ch4\access\E06_ProtectedManipulation.java d:\JavaProjects\TIJ4>java -cp . ch4.access.E06_ProtectedManipulation
Last edited by pbrockway2; 07-30-2011 at 07:41 AM.
- 07-31-2011, 06:00 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Thank you sir ! It works like a charm, you are the best. Now, is there a way to modify the CLASSPATH so I won't have to provide the compiler with these arguments every time? Thanks!
- 07-31-2011, 06:20 PM #4
Add a . (dot) to the CLASSPATH variable.is there a way to modify the CLASSPATH
The . refers to the current directory.
For the package path, you will always have to enter that.
- 07-31-2011, 06:23 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Ok. Thank you Norm, greatly appreciated.
- 07-31-2011, 09:36 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I think the dot was already part of the CLASSPATH setting, so the commands I posted may well have been OK without the dash-cp-space-dot. For what it's worth, I don't use (rely on) CLASSPATH as I find one size does not fit all. Also it can solve problems before they're created to actually have to think about what you want the classpath to be.
As your package structure gets more complicated - and as you add setting source and destination directories into the mix - typing the entire command at the command line becomes a pain. That's when things like ANT or an IDE become very useful. It's still good to know what the command line options are, however, so that you can understand the options provided by other interfaces.
- 07-31-2011, 09:42 PM #7
Having the CLASSPATH set to a folder that you have forgotten about because your project has moved on can cause suprises.
A lot of people think you should not use the CLASSPATH variable. Its hidden away and could be forgotten. Its bitten some students twice in the last couple of weeks. They had left off the .
Scripts and batch files can provide the full command line you need to execute a program.
Similar Threads
-
ANT build script, problem with packaging using rapc
By bartosz666 in forum CLDC and MIDPReplies: 0Last Post: 04-19-2011, 02:20 PM -
classpath problem, out of ideas
By edzet in forum Advanced JavaReplies: 16Last Post: 05-14-2010, 12:38 PM -
classpath problem
By Nifras in forum New To JavaReplies: 6Last Post: 11-28-2009, 04:49 PM -
classpath problem
By shwein in forum New To JavaReplies: 4Last Post: 04-01-2009, 12:32 PM -
Problem with CLASSPATH for STRUTS
By bhupal4all in forum Web FrameworksReplies: 2Last Post: 11-07-2008, 05:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks