I have a file Foo.java which compiles with no errors. But when I add the single line
import static org.junit.Assert.assertNotEquals;
This causes the following error from my ant build:
compileTests:
[javac] Compiling 27 source files to C:\example\bin
[javac] C:\example\src\Foo.java:7: error: cannot find symbol
[javac] import static org.junit.Assert.assertNotEquals;
[javac] ^
[javac] symbol: static assertNotEquals
[javac] location: class
[javac] 1 error
I am using JUnit 4.4. junit-4.4.jar is included in the classpath for the javac task.
I have tried running different compiler versions with the flags -Dbuild.compiler=javac1.7 and -Dbuild.compiler=javac1.8 but it makes no difference. I still get the exact same error.
assertNotEquals is the only static import from Assert that causes this error.
I have already seen this post Java static import causing compile error. Probable compiler bug?
and I tried importing Assert on its own and prepending each assert statement with Assert. like this post suggested. I got the same error, this time at the method call for Assert.assertNotEquals()instead of the import statement.
The import successfully compiles and runs on Eclipse, however.
Does anyone have any ideas about the cause of this? I'd like to get to the bottom of this instead of having to implement a workaround solution.
You are using Junit 4.4. AssertNotEquals wasn't added until 4.11
JUnit 4.11 release Notes
If you update to the latest JUnit jar. the compiler error should go away. Eclipse is probably using its own built in version that has the latest methods.
Related
This question already has an answer here:
getting error when I compile the Java code using package in commandline?
(1 answer)
Closed 8 years ago.
I have been using Eclipse lately, where compiling and running the program is very simple. Not much needs to be done in setting the classpath. But apparently that is not the case when it comes to running them from commandLine. when I try compiling from terminal, I am having various errors. I am pasting an image of my package structure of the project cp125_soln. Now I want to compile Invoice.Java in the com.scg.domain package,
I tried
javac src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/domain/Invoice.java:17: error: package com.scg.util does not exist
import com.scg.util.StateCode;
.......................//long error message
This means I do not have com.scg.util.* in my classpath. so I tried
javac -cp src/main/java/com/scg/util/* src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/util/ListFactory.java:8: error: package org.slf4j does not exist
import org.slf4j.Logger;
^
src/main/java/com/scg/util/ListFactory.java:9: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
^
src/main/java/com/scg/util/ListFactory.java:11: error: cannot find symbol
import com.scg.domain.ClientAccount;
^
symbol: class ClientAccount
location: package com.scg.domain
................... // long error message
I read different articles on how classpath works and how to provide it in command-line. but when it comes topackage level structures, I am not able to find a good tutorial on how to COMPILE and RUN packages. If a little help could be provided here on the propery way to compile and run these kind of packages, it will be very helpful.
javac src/main/java/com/scg/domain/Invoice.java
Try this:
cd src/main/java
javac com/scg/domain/Invoice.java
I am using Lucene 3.4.0, hibernate (hibernate-all.jar), ANT and TestNG to run some test...
Most of all works right now, but since I have implemented my lucene part nothing seems to work when I use ant command... I'm kind of lost here.
Because everything works perfectly locally. I can run my test, compile, etc. As soons as, I try to build with ant, I get this error "cannot find symbol" for contructor StandartAnalyzer(version.), etc...
But like I said, everything works on eclipse... At first, I had an method not found error, but fixed it because hibernate is not compatible with the latest version of lucene. So I upgraded hibernate... but now, I get this.
Thank you. If you need more information plz ask.
$ ant explode
Buildfile: c:\DuplicateBugFinder\dbf\build.xml
init:
groovy.compile:
groovy.copy:
compile:
[javac] c:\DuplicateBugFinder\dbf\build.xml:156: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=l
st; set to false for repeatable builds
[javac] Compiling 27 source files to c:\DuplicateBugFinder\dbf\exploded-archives\dbf.ear\dbf_jar
[javac] c:\DuplicateBugFinder\dbf\src\hot\com\rim\sts\dbf\business\service\classification\LuceneClassificationAlgorithm.java:
6: cannot find symbol
[javac] symbol : constructor StandardAnalyzer(org.apache.lucene.util.Version)
[javac] location: class org.apache.lucene.analysis.standard.StandardAnalyzer
[javac] Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
[javac] ^
[javac] c:\DuplicateBugFinder\dbf\src\hot\com\rim\sts\dbf\business\service\classification\LuceneClassificationAlgorithm.java:
2: cannot find symbol
[javac] symbol : constructor QueryParser(org.apache.lucene.util.Version,java.lang.String,org.apache.lucene.analysis.Analyzer
[javac] location: class org.apache.lucene.queryParser.QueryParser
[javac] new QueryParser(Version.LUCENE_34, "title", analyzer)
[javac] ^
[javac] c:\DuplicateBugFinder\dbf\src\hot\com\rim\sts\dbf\business\service\classification\LuceneClassificationAlgorithm.java:
9: cannot find symbol
[javac] symbol : constructor IndexSearcher(org.apache.lucene.store.Directory,boolean)
[javac] location: class org.apache.lucene.search.IndexSearcher
[javac] searcher = new IndexSearcher(index, true);
[javac] ^
[javac] c:\DuplicateBugFinder\dbf\src\hot\com\rim\sts\dbf\business\service\classification\LuceneClassificationAlgorithm.java:
00: cannot find symbol
[javac] symbol : method search(org.apache.lucene.search.Query,org.apache.lucene.search.TopScoreDocCollector)
[javac] location: class org.apache.lucene.search.IndexSearcher
[javac] searcher.search(q, collector);
[javac] ^
[javac] 4 errors
BUILD FAILED
c:\DuplicateBugFinder\dbf\build.xml:156: Compile failed; see the compiler error output for details.
It's probablly a difference in the classpathes used by Eclipse and by Ant respectively. Check what jars are you exactly using inside Eclipse (what libraries are added to the project) and then what jars you use in your declared Ant classpath when you build with that. Also check that you're using the same JDK in Eclipse and Ant. If either of the two differ, change Ant ones to be like Eclipse (since that's the environment that does work)
Check the ant classpath for multiple versions of Lucene library. Do ant -diagnostics to check the ant environment.
Ok fixed it. I needed to add the jar file in my lib/test (I'm using seam framework). In eclipse, everything was working because I was pointing to the jar in my run configurations for TestNG. When I was running the command "ant test" no jar was found...
So thanks to everyone.
Plus, for those of you who wants to make use of lucene and seam make sure too have no version incompatibilty. I had this problem for a long time. Finally, I found out that I had to use an older version of lucene (2.4.1) to work with seam 2.2. I didn't check yet for lucene 3.4 and seam 3...
I'm including a rather large JAR file during compilation that I'm generating using Altova MapForce. The JAR file was around 65MB and compilation was working fine. I needed to add a bunch more mappings, so I did, regenerated my JAR, and tried building again. The JAR is now around 80MB.
Now I get all kinds of "cannot find symbol" error messages even though I've opened the JAR to verify that the symbols are there! Am I hitting some upper limit on JAR sizes? There are over 75,000 classes inside the JAR file.
What's weird is that Eclipse auto-completion works just fine with the new JAR; I only get these errors when building.
I'm using Ant if that makes any difference.
Thanks,
Brian
EDIT: Something else strange: I turned on the verbose output from javac. It seems that I start getting compile errors even before all of the classes are loaded:
[javac] [loading com\mycompany\myproject\*************************.class)]
[javac] [loading com\mycompany\myproject\*************************.class)]
[javac] [loading com\mycompany\myproject\*************************.class)]
[javac] [loading com\mycompany\myproject\*************************.class)]
[javac] [loading com\mycompany\myproject\*************************.class)]
[javac] C:\Users\*************************.java:38: cannot find symbol
[javac] symbol : class *************************
[javac] location: package com.mycompany.myproject.*************************
[javac] import com.mycompany.myproject.*************************;
[javac] ^
[javac] [loading com\altova\TraceProvider.class(com\altova:TraceProvider.class)]
[javac] [loading com\altova\io\StringInput.class(com\altova\io:StringInput.class)]
[javac] [loading com\altova\io\StringOutput.class(com\altova\io:StringOutput.class)]
It would be helpful to see the specific compiler errors you're getting, your classpath, etc. If it works from eclipse and not in ant, it's likely to be a classpath issue, i.e. your ant script is not setting the same classpath as your project settings in eclipse has.
I work on one project that has a jar file larger than 80MB no problem, and I don't think there's an upper limit on number of classes in a jar file or anything like that. the size of the jar and the number of classes it contains is unlikely to be your problem.
you can try writing a simple test main program that just imports the classes in question, and compile that with javac with only that jar on your classpath. if that test doesn't work, you know you've got a problem with the jar you're using. if it doesn't work, is has got to be either an ant issue or similar.
can you post your ant build target, and the verbose output of ant?
Well, I've solved the problem. All I did was regenerate my JAR (I didn't change anything!) and it worked. Seems like it was just a fluke.
Sorry to waste everyone's time!
I had exactly this problem, when building via Ant on Jenkins.
The class which is not found is shown loading after the error: "Cannot find symbol" shows up in the verbose output. Tried rebuilding numerous times, no joy.
Finally, I went to the class importing the class not found, and changed the order of imports so the problem class was nearer the top. This is a temporary fix, as soon as someone organizes the imports in Eclipse, they will be sorted in an order that Javac will fail to load. We may have to rename the class, so it sorts to the top of the imports.
After some more research, I found this is related to bug 7101822 in Java, where eager loading of the classes can result in "Cannot find symbol" error.
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7101822
I am asking this question on stackoverflow as this is the suggested guava question forum indicated on the guava website (http://code.google.com/p/guava-libraries/).
I want to try out some of the unreleased new features of google guava (like the cool MoreExecutors.listeningDecorator feature). As I haven't found a google guava nightly build, I decided to try to create the jar myself. Unfortunately I am seeing the following exception from javac (on Mac OS 10.6.8) when I try to build:
[javac] An exception has occurred in the compiler (1.6.0_24). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
[javac] java.lang.NullPointerException
[javac] at com.sun.tools.javac.comp.Check.checkCompatibleConcretes(Check.java:1215)
[javac] at com.sun.tools.javac.comp.Check.checkCompatibleSupertypes(Check.java:1567)
[javac] at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:2674)
[javac] at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:2628)
[javac] at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:2564)
[javac] at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1036)
[javac] at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:765)
[javac] at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:730)
.
.
I see that I am not alone in this error:
http://gump.zones.apache.org/gump/public/google-guava/google-guava/gump_work/build_google-guava_google-guava.html
I understand this looks like a Java compiler bug, but is there perhaps a workaround to get guava to compile? Or if not, is there a recent guava nightly build somewhere which I could use instead?
I can't see any error on the page you've linked to (it shows a successful build). Maybe this has been fixed? Or did you possible attach the wrong URL?
I'd try OpenJDK.
If this is a bug in the com.sum.* libraries then you may find that OpenJDK does not suffer with the same problem.
Has anyone successfully used Ant4Eclipse (http://www.ant4eclipse.org/) in combination with Project Lombok (http://projectlombok.org/)?
Lombok provides annotations for removing boilerplate code; however, it doesn't appear to play nicely with Ant4Eclipse (headless compilation of Eclipse projects). For instance, the following Lombok sample compiles fine in Eclipse and javac:
import lombok.Getter;
public class LombokTest {
private #Getter String foo;
public LombokTest() {
String s = this.getFoo();
}
}
But compiling with Ant4Eclipse's <buildJdtProject> yields the following:
[javac] Compiling 1 source file
[javac] ----------
[javac] 1. WARNING in C:\dev\Java\workspace\LombokTest\src\LombokTest.java (at line 4)
[javac] private #Getter String foo;
[javac] ^^^
[javac] The field LombokTest.foo is never read locally
[javac] ----------
[javac] 2. ERROR in C:\dev\Java\workspace\LombokTest\src\LombokTest.java (at line 8)
[javac] String s = this.getFoo();
[javac] ^^^^^^
[javac] The method getFoo() is undefined for the type LombokTest
[javac] ----------
Has anyone successfully used these libraries together?
Thanks!
Edit: sample project demonstrating the issue
I eventually managed to compile a lombok project with ant4eclipse by delomboking the project first with
<delombok verbose="true"; encoding="UTF-8"
to="${workspaceDirectory}/myproject/src"
from="${workspaceDirectory}/myproject/src-original" />
IIRC in order to use Lombok in Eclipse, there's a parameter required at start-up to introduce Lombok's Java Agent into the JDT compile process. This is normally specified in the eclipse.ini file. If Ant4Eclipse does not also make use of those parameters (I don't see why it would) you may have to specify the -javaagent:lombok.jar parameter for that too.
DISCLAIMER: this is only a guess.
Supplied project does not compile out of the box with Eclipse Java EE 3.5.2 using Java 6. The errors in Eclipse are the same as those given by ant4eclipse.