0

I can't compile my Program.java from the command line (only in Eclipse). When I want to compile it with

javac Program.java

"cannot find symbol" errors occur at JUnit classes.

Eclipse has the JUnit classes in it's plugins, but to compile it on my own I would need to somehow compile my JUnit.jar with the program. How can I do that? It doesn't seem to work with

javac -cp absolutePathTo\JUnitJar Program.java

Or is this problem maybe caused because the JUnit classes are not implemented in my (nested) new Thread classes?

    C:\Documents and Settings\xxxx\Desktop\eclipse\xxxx\Program\src\da
    ta>javac -cp junit-4.10.jar Testworks.java
    Program.java:81: package org.junit.runner does not exist
    import org.junit.runner.Description;
                           ^
    Program.java:82: package org.junit.runner does not exist
    import org.junit.runner.JUnitCore;
                           ^
    Program.java:83: package org.junit.runner does not exist
    import org.junit.runner.Request;
                           ^
    Program.java:84: package org.junit.runner does not exist
    import org.junit.runner.Result;
                           ^
    Program.java:85: package org.junit.runner.notification does not exist
    import org.junit.runner.notification.Failure;
                                        ^
    Program.java:86: package org.junit.runner.notification does not exist
    import org.junit.runner.notification.RunListener;
                                        ^
    Program.java:253: cannot find symbol
    symbol  : class JUnitCore
    location: class data.Program
            JUnitCore jCore; //-> Core Runner - has no pleaseStop()
            ^
    Program.java:254: cannot find symbol
    symbol  : class RunListener
    location: class data.Program
            RunListener jRl;
            ^
    Program.java:255: cannot find symbol
    symbol  : class Request
    location: class data.Program
            Request jRq;
            ^
   Program.java:2167: cannot find symbol
    symbol  : class RunListener
    location: class data.Program
            class RlOne extends RunListener{
                                ^
    Program.java:2170: cannot find symbol
    symbol  : class Description
    location: class data.Program.RlOne
                            public void testRunStarted(Description descRun)
                                                       ^
    Program.java:2179: cannot find symbol
    symbol  : class Description
    location: class data.Program.RlOne
                            public void testStarted(Description descStart)
                                                    ^
    Program.java:2185: cannot find symbol
    symbol  : class Description
    location: class data.Program.RlOne
                            public void testFinished(Description descFinish)
                                                     ^
    Program.java:2202: cannot find symbol
    symbol  : class Failure
    location: class data.Program.RlOne
                            public void testFailure(Failure failure)
                                                    ^
    Program.java:2211: cannot find symbol
    symbol  : class Description
    location: class data.Program.RlOne
                            public void testIgnored(Description descIgno)
                                                    ^
    Program.java:2221: cannot find symbol
    symbol  : class Result
    location: class data.Program.RlOne
                            public void testRunFinished(Result result)
                                                        ^
    Program.java:2422: cannot find symbol
    symbol  : variable Request
    location: class data.Program.ThirdThread
                                    jRq = Request.aClass(cRun);
                                          ^
    Program.java:2426: cannot find symbol
    symbol  : variable Request
    location: class data.Program.ThirdThread
                                    jRq = Request.method(cRun, comb_meth.getSelected
    Item().toString());
                                          ^
    Program.java:2584: cannot find symbol
    symbol  : class JUnitCore
    location: class data.Program
                            jCore = new JUnitCore();
                                        ^
    19 errors

The java command output when I want to start it by the Eclipse compiled Program.class:

C:\Documents and Settings\xxxx\Desktop\eclipse\xxxx\Program\bin\da
ta>java Program
Exception in thread "main" java.lang.NoClassDefFoundError: Program
Caused by: java.lang.ClassNotFoundException: Program
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Program.  Program will exit.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Arian
  • 3,183
  • 5
  • 30
  • 58
  • "It doesn't seem to work" isn't very precise - what *exactly* happens? That should work fine... – Jon Skeet Mar 13 '12 at 11:04
  • just posted the log, no matter how I try to add the JUnit jar this happens – Arian Mar 13 '12 at 11:04
  • also I cannot run the by eclipse compiled Program.class with javaw Program > NoDefClassFound error – Arian Mar 13 '12 at 11:06
  • Where is junit-4.10.jar? (And you should post the details of the NoClassDefFoundError as well - it's probably more of the same, i.e. needing to fix your classpath.) – Jon Skeet Mar 13 '12 at 11:11
  • have added it too. I moved the junit-4.10.jar to the src folder and also copied one in the data package next to the Program.java – Arian Mar 13 '12 at 11:14
  • Okay, let's try to tackle one problem at a time. Your javac command *should* have worked... – Jon Skeet Mar 13 '12 at 11:20
  • should but it doesn't as seen by the 19 errors above – Arian Mar 13 '12 at 11:24
  • Well there's certainly going to be a simple explanation for this. Please show a directory listing followed by the command line and output. – Jon Skeet Mar 13 '12 at 11:55

3 Answers3

1

While using javac command with external jar, you should use

javac -classpath path-to-external-jar/jarname1.jar;/path-to-external-jar2/jarname2.jar Program.java

same for java command:

java -classpath path-to-external-jar/jarname1.jar;/path-to-external-jar2/jarname2.jar Program

valiano
  • 16,433
  • 7
  • 64
  • 79
Rahul Borkar
  • 2,742
  • 3
  • 24
  • 38
  • *C:\Documents and Settings\x\Desktop\eclipse\x\Program\src>ja vac -classpath "C:\Documents and Settings\x\Desktop\eclipse\x\Program\src\data\junit-4.10.jar" data/Program.java* – Arian Mar 13 '12 at 11:50
  • data\Program.java:81: package org.junit.runner does not exist import org.junit.runner.Description; – Arian Mar 13 '12 at 11:52
0

Now I got it to work.

I renamed junit-4.10.jar to junit.jar, moved it to another folder and somehow this solved the problem:

javac -classpath "C:\Documents and Settings\x\Desktop\eclipse\x\Program\extres\junit.jar" src\data\Program.java

BTW:

When I'm in C:\Documents and Settings\x\Desktop\eclipse\x\Program\ it also works with:

javac -classpath extres\junit.jar src\data\Program.java
valiano
  • 16,433
  • 7
  • 64
  • 79
Arian
  • 3,183
  • 5
  • 30
  • 58
-3

You really need to learn more on the topic of the Java CLASSPATH. Your errors, both at compile and at run time, are due to you not providing the complete classpath.

vagelis
  • 334
  • 3
  • 9
  • What is it exactly that you don't like about my answer? Do you expect people to post commands you can copy-paste to do your work? Have we come to THIS required level of hand-holding?? – vagelis Mar 13 '12 at 12:35