This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes junit classpath issue, I think Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "junit classpath issue, I think" Watch "junit classpath issue, I think" New topic
Author

junit classpath issue, I think

Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Deep apologies for this really really basic question.

In going through the Ant/JUnit tutorial here:
http://www-106.ibm.com/developerworks/library/j-ant/?dwzone=java
...I'm stuck trying to get tests using junit to compile. The main source files compile, but the tests fail, with errors like "package junit.framework doesn't exist", and "package junit.textui doesn't exist."

The junit distribution is here (I'm on win2k):
C:\Program Files\JavaPackages\junit3.8.1\
junit.jar is directly inside there.

I've added junit.jar to the system classpath, so it's like this:
C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;c:\;c:\dell;C:\WINNT\Java\Classes\RxAssistant.zip;.;C:\Servlets+JSP;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\jsp-api.jar;C:\Program Files\JavaPackages\junit3.8.1\junit.jar;

What am I missing here?


While I'm at it, a couple of related questions:

- Not that I want to do it this way, but I tried copying junit.jar to the project directory, the project's src directory, and theproject/src/test, and it still doesn't get found. Why? Shouldn't that remove the dependency on the system class path?

- Is there a standard location on windows for java packages like junit that are likely to get used in many projects?

Thanks very much, trying not to stay a newb for too long...
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
you'v to tell javac and java where to find junit.jar

supposing you'v it inside a folder called junit3.8.1 under C, you can use this from command line:
prompt>set JUNIT_HOME=C:\junit3.8.1
prompt>set CLASSPATH=C:\junit3.8.1\junit.jar;.

you can proceed in similar fashion with ant:
prompt>set ANT_HOME=C:\apache-ant-1.6.2


java amateur
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Originally posted by miguel lisboa:
you'v to tell javac and java where to find junit.jar

supposing you'v it inside a folder called junit3.8.1 under C, you can use this from command line:
prompt>set JUNIT_HOME=C:\junit3.8.1
prompt>set CLASSPATH=C:\junit3.8.1\junit.jar;.

you can proceed in similar fashion with ant:
prompt>set ANT_HOME=C:\apache-ant-1.6.2


Thanks for replying, but I really want to break this down so I fully get it.

Are you saying that my system class path is set incorrectly? It looks right to me; could you (or anyone else kind enough to look) check it? I posted where the files are, and what my classpath is. Or are you saying that the system classpath doesn't matter?

FWIW, ant has been working fine all along, with the system environment var ANT_HOME set to the ant install directory.

From my understanding of the junit docs, I don't think JUNIT_HOME is an environment variable that needs to be set. It just looks like they're referring to the directory where junit is installed, not an actual var. Am I wrong?

In any case, I tried adding JUNIT_HOME to the system classpath, pointing at the junit install dir; no difference.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Are you sure what you've shown about is the environment variable CLASSPATH, and not PATH? They're totally different and unrelated. The first bunch of entries looks like what PATH should look like, and then there are bunch of CLASSPATH-like things stuck on at the end. PATH is used by Windows to find programs; Java ignores it. CLASSPATH is used by Java to find classes; Windows ignores it.

You might want to go have a look at

http://faq.javaranch.com/view?HowToSetTheClasspath


[Jess in Action][AskingGoodQuestions]
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
System classpath, copied directly out of system/properties/advanced/:

C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;c:\;c:\dell;C:\WINNT\Java\Classes\RxAssistant.zip;.;C:\Servlets+JSP;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\jsp-api.jar;C:\Program Files\JavaPackages\junit3.8.1\junit.jar;

I didn't put any of those entries in manually except for the junit one. Everything else in there came that way, or was set up by some installer. I don't know why system32 should be in the classpath, but it is.

BTW, are you saying that I understand the principles of how this should work correctly?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, sounds like you're doing the right thing.

One more obvious thing that nonetheless gets people all the time: changed environment variables don't propagate to existing programs, so after changing this setting, you'd have to close your command window and open a new one.

If that's not it, try compiling like this:

javac -classpath ".;C:\Program Files\JavaPackages\junit3.8.1\junit.jar" FooTest.java

Hmmm. In typing that I had to add the quotes because "Program Files" has that at space in it. I don't actually know how Java deals with those (I've been blessedly Windows-free for years) but it's possible that there's a problem right there. You might want to consider just putting junit in c:\junit3.8.1 (or, say, c:\JavaPackages\junit3.8.1) and trying again.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Also, the classpath can only pick up JARs and ZIPs by naming them directly -- not by naming the directory they are in. If "C:\Servlets+JSP" contains a bunch of .class files in a hierarchy, then that's okay. But if it contains some JARs, java and javac will not look at them.

However, I'd seriously recommend that you backtrack a short bit. One of the main advantages of using Ant (or any build system) is reproducability and not having to count on random environment settings as much as possible. Instead of setting the system classpath which other installers might change without you knowing it, the JARs a project needs should be explicitly set up in the build.xml file.

Typically you create several <path>s with IDs that you can reference throughout the script: shared path (used by all other paths), compile-only path, runtime-only path, and testing-only path. The last three include the first one so you don't have to replicate the JAR references too much. Or to avoid that complexity, just create a single <path> with all libraries you need for all modes of building your project.

The Ant tutorial should mention this as it's very standard for Java projects. This way you're free to ignore your system classpath entirely. This is key once you start working on more than one project as they'll inevitably need different sets of libraries.
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Um, I don't mean to sound like a complete idiot, but I *know* there were other posts on the end of this thread that I can't see any more.

As it happens, I still have a window open where they're visible. After David Harkness's post, there were two more by me, two by Nigel Browne. Besides that, I think but I'm not certain, there was one more by me that I don't have in that old window.

I don't see any "next page" btns or anything like that that stay within this thread, and besides, they all fit on the first page before. Seems unlikely that part of this thread got moved somewhere else. Is something about what or how I'm asking about this wrong-headed enough to get it killed? Doesn't seem like it.

What's going on here?
[ April 20, 2005: Message edited by: Dave Merrill ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
It looks like the Ranch took a dive an we lost most of today's posts.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Dave, my two posts have gone I can rewrite my comments if needed
Did you get things working ?
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
[Repost of a missing post of mine, so we keep some continuity. - drm]

If you don't mind, let me start at the absolute basics of junit install and test; please poke me in the head if you see something out of line. Apologies once again for the basicness of these questions. I'm running windows 2000, fyi.

This is from the junit readme:
# unzip the junit.zip file
# add junit.jar to the CLASSPATH. For example: set classpath=%classpath%;INSTALL_DIR\junit3\junit.jar
# test the installation by using either the batch or the graphical TestRunner tool to run the tests that come with this release. All the tests should pass OK.

Notice: that the tests are not contained in the junit.jar but in the installation directory directly. Therefore make sure that the installation directory is on the class path

* for the batch TestRunner type:

java junit.textui.TestRunner junit.samples.AllTests

So, I unzipped junit to:
C:\Program Files\JavaPackages\junit3.8.1\

Added the following to the system classpath:
C:\Program Files\JavaPackages\junit3.8.1\junit.jar;

Started a cmd prompt in the junit3.8.1 directory, and typed:
set classpath=%classpath%;C:\Program Files\JavaPackages\junit3.8.1
That would be "making sure the installation directory is on the class path", yes?

I then typed:
java junit.textui.TestRunner junit.samples.AllTests

This produces a NoClassDefFoundError: junit/textui/TestRunner


What's wrong with this picture? It looks like I'm following the install instructions verbatim, and bombing out. If there's something wrong or missing in the instructions, please clue me, and maybe we can get the docs changed (though I don't know how to do that either).

Thanks again.

[ April 21, 2005: Message edited by: Dave Merrill ]
[ April 21, 2005: Message edited by: Dave Merrill ]
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Arrgghh. Sorry to be thick.

Originally posted by Nigel Browne (in one of the missing posts):
No you must add the jar file to the classpath. You should have typed:
set classpath=%classpath%;C:\Program Files\JavaPackages\junit3.8.1.jar
If that's where the jar file is.

But I already added the jar to the system classpath, as I said. Am I right that that's taken care of then? The jar is now known to java? If not, why not? What did adding the jar to the system classpath do, if not that?


The set statement I typed in the cmd prompt was in response to the instruction step saying:
"Notice: that the tests are not contained in the junit.jar but in the installation directory directly. Therefore make sure that the installation directory is on the class path"

The way I understand this is that even though the junit jar itself is now known, the actual tests to run are not, so you need to add the dir where they are to the classpath. This isn't about the infrastructure junit provides for testing other code, just the tests supplied with it. Just like with tests for any other class, they're specific to the module under test, which just happens to be junit itself in this case. Right?


I believe that one of the lost posts asked for a dump of the session classpath, which leads me to another question. From that cmd prompt, when I type echo %classpath% or set classpath, only the one adobe thing that's in the classpath for my user name and the junit entry I just set appear. But ant, which is in the system classpath, is clearly in the effective classpath too, since it runs. I'm not a dos guru, but it seems like the echo statement only shows the user entries, plus whatever changes you made in that cmd session; the system classpath is active, but not visible this way. Do I have that right? Is there any way to get a cmd window to show the whole effective classpath, system, user, and session?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Here's my comments again:

From a command prompt, type:

set CLASSPATH

and look at the results/post them here. If the .jar file is listed in the path, then we need to look somewhere else (like, is the .class file in the .jar file to start with?) Otherwise, your classpath is not set properly.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Originally posted by Joel McNary:
From a command prompt, type:

set CLASSPATH

and look at the results/post them here.

Thought I covered that in my last post, but here it is:
CLASSPATH=c:\Program Files\PhotoDeluxe HE 3.1\Adboe Connectables;;C:\Program Files\JavaPackages\junit3.8.1

The only entries that show are the ones under my user name, with the session-specific changes appended to the end. The system classpath entries aren't visible, but they're clearly in effect, because ant runs.
[ April 21, 2005: Message edited by: Dave Merrill ]
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Here's what Nigel posted about setting classpaths using ant. Just to be clear, that's my plan too, but I want to straighten out my own understanding of these classpath issues before I have ant automate it.

When I write Ant scripts I always set the classpath in the script. I do this as follows



I then use the classpath when I build and run, for example



It is easy to control the elements on your classpath using Ant.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
When you installed ANT you added the bin directory of ANT to your PATH variable. This is why it is functioning.
JUnit requires you to add junit.jar to your classpath so that java can run your tests. You also need to add the istallation directory to your classpath to run the examples as they are not contained in junit.jar. You should be able to get thing s up and running with the following
[ April 21, 2005: Message edited by: Nigel Browne ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Originally posted by Dave Merrill:

Thought I covered that in my last post, but here it is:
CLASSPATH=c:\Program Files\PhotoDeluxe HE 3.1\Adboe Connectables;;C:\Program Files\JavaPackages\junit3.8.1

The only entries that show are the ones under my user name, with the session-specific changes appended to the end. The system classpath entries aren't visible, but they're clearly in effect, because ant runs.

[ April 21, 2005: Message edited by: Dave Merrill ]



...and the problem is that the .jar files containing the class is not in the classpath. It is not enough to add the directory where the .jar resides; you must specify the .jar file itself.

C:\Program Files\JavaPackages\junit3.8.1\junit3.8.1.jar (or some such)
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
I'm finally getting somewhere.

It appears that the lone classpath entry that Adboe's installer put under my user name supercedes everything in the system classpath, as if the system one didn't exist. I moved the Adobe entry into the system, then deleted the user one, and things are (somewhat) running now. I still do have an error I'll ask about in a sec, but at least it doesn't seem so completely irrational now. Kind of rude on the part of that Adobe installer, I'd say.


The error I'm getting now is this:

If I'm reading that right, junit did run the tests, meaning junit itself is now functional, but that one test failed. Is that right?

[EDIT]On rereading the error, I think I was wrong. JUnit can run overall, but the problem is that this one test class isn't getting found. Is that right? Why wouldn't it get found?[/EDIT]

If so, here's the code for the failing test class. I'm new enough at this that while the code seems pretty simple, it's not penetrating what the issue is. Can anyone explain a bit about what's going on?


Thanks again, very much, for all the help.
[ April 21, 2005: Message edited by: Dave Merrill ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Great to see you got things working. I think you should start posting JUnit questions in the Testing forum.
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Will do.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The test which is failing is testJarClassLoading which is in your TestCaseClassLoaderTest. This is the first place to look.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: junit classpath issue, I think
 
Similar Threads
SCJP 1.4
Problem Pooling prepared statements
1.2 & 1.4
How i can prepare
One junit3.8.1 self-test failing