Javac compiler bug with class name antbuild - java

I was trying to do an ant build that compiled a java project source code when i ran into a problem i could not explain. First my ant target:
<target name="compile-companymanage" depends="compile-company">
<mkdir dir="${companymanagebin}"/>
<javac destdir="${companymanagebin}">
<src path="${companymanagesrc}" />
<classpath refid="companyManage.classpath"/>
</javac>
</target>
After running that i get the error:
[javac] project\src\com\company\manage\vo\InvoiceL‌ineSupplierVM.java:31: error: class InvoiceLineSupplierVM is public, should be declared in a file named InvoiceLineSupplierVM.java
[javac] public class InvoiceL‌ineSupplierVM
[javac] ^
[javac] 1 error
We have tried it with different projects and they compile just fine. Hope someone can help me.
We have tried renaming the file and class with the same name.

Adding the answer for completeness sake and to simplify the lives of other people which will encounter this error in the future.
Most likely you have a non-printable character in one of your names, either your class name or your file name. This will cause a mismatch between what the compiler has or expects.
To solve this problem, what one usually does is to rename the files and classes involved. It is important that when renaming, you do not copy and paste the name but rather type it in. This will make sure that non-printable characters do not end up in the names of the file and class as happened.

Related

javac compilation issue with packages

Given this (game.Game.java, game.Player.java, game.io.InputConsole.java):
src
|_game
Game.java
Player.java
|_io
InputConsole.java
I've been trying to compile this project on console with javac (trying out various solutions found on SO and the internet) but I keep failing. I also tried using a batch file, but in either case, all I get are "cannot find symbol" errors.
Since almost all answers to related questions suggest to use a build tool like Ant or Maven, I decided to give it a try with Ant (first time). This is what my Ant file looks like:
<project default="run" name="Tic_Tac_Toe">
<target name="run" depends="compile">
<java classname="game.Main">
<classpath path="bin" />
</java>
</target>
<target name="compile">
<javac srcdir="." destdir="bin"/>
</target>
</project>
This is an excerpt of the output:
C:\Users\...\src\game\io\InputConsole.java:10: error: cannot find symbol
public Player getPlayer(Sign sign) {
symbol: class Player
location: class InputConsole
PS: It works when I move InputConsole.java into the game package. So I'm sure the classes themselves are fine.
I think the problem is either that I'm failing giving the compiler a proper path or my package structure is wrong.
You need to import in both. There is no hierarchy among packages and in spite of appearance game.io is not a "sub-package" of game, because there is no such thing as a sub-package.
For classes in package game; you need import game.io.InputConsole;. In package game.io; you need import game.*;.
Note that * is just convenience, it is probably better to import each referenced class individually.

Jenkins Project Can't Find Packages; build-project target fails

My project compiles and runs on my local machine, but on Jenkins the build-project target fails to compile due to being unable to find my packages. I can see that the jars that I think I need are in my ./bin/ folder, so I anticipate there is something about classpaths or my build-project target that is causing the issue.
build.xml -> build-project:
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}" />
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src" />
<classpath refid="sierraMadreTestScripts.classpath" />
</javac>
</target>
error message excerpt on Jenkins:
build-project:
[echo] sierraMadreTestScripts: /data/builds/jenkins/workspace/build.xml
[javac] Compiling 1 source file to /data/builds/jenkins/workspace/bin
[javac] /data/builds/jenkins/workspace/src/testScripts/TranslatorWorkflow.java:3: error: package org.junit does not exist
[javac] import static org.junit.Assert.fail;
[javac] ^
I can post my full build.xml, ivy.xml, ivysettings.xml, or whatever else would be necessary to diagnose the problem. Sorry if this is really basic; Ivy is still rather arcane to me. I'm running my local project within Eclipse.
The root cause of your problem is outlined in the following question:
Running ant build gives "package org.junit does not exist"
In short IDEs like Eclipse automatically put junit in your classpath..... Ivy is actually designed to help solve this problem and used properly it can be used to manage all aspects of your project's classpaths.
Without access to your build logic I must guess, but it looks like you're building both code and tests using a single javac? You have a classpath called "sierraMadreTestScripts.classpath", the next question is how is this created and managed. Your question implies that ivy is somehow involved :-)

Ant compiler warnings for specific class file

I want to ignore warnings from ant which are thrown by an specific file.
It is not mandatory why there are warnings i only want to find a way that any ignore the warnings form an specific class file.
Is there a way to do that?
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac
classpathref="project.classpath"
bootclasspath="${javac.bootclasspath}"
compiler="${javac.compiler}"
debug="${javac.debug}"
deprecation="${javac.deprecation}"
destdir="${classes.dir}"
fork="${javac.fork}"
memoryMaximumSize="${javac.memoryMaximumSize}"
nowarn="${javac.nowarn}"
srcdir="${source.dir}"
source="${javac.source}"
target="${javac.target}"
encoding="UTF-8"
>
<compilerarg value="-XDignore.symbol.file"/>
</javac>
</target>
I take it you mean "suppress compilation warnings from javac when running an Ant script"?
You don't supply an example of a warning, but in general you could look into the #SuppressWarnings annotation. Sadly, only "unchecked" is required byt the JLS, while all others are implementation dependent - you can try a
localhost:~$ javac -X
-Xlint:{all,cast,deprecation,divzero,empty,unchecked,fallthrough,path,
serial,finally,overrides,-cast,-deprecation,-divzero,-empty,-unchecked,
-fallthrough,-path,-serial,-finally,-overrides,none}
to see the ones supported on your chosen JDK.
Edit: It is not possible to suppress the "internal proprietary API" type warnings in this manner, cf. this Bug ID. It should, however, be possible with the (undocumented) -XDignore.symbol.file command line option for javac (see eg. bug 6544224).
The real solution is of course to not use these APIs...
Cheers,
javac ant task has nowarn property to switch off all the warnings at the time of compiling. But to mute warnings from one specific class, you will have to modify your java file only.
Here it goes
http://ant.apache.org/manual/Tasks/javac.html
Add #SuppressWarnings to your class definition. Example:
#SuppressWarnings
public class MyClass {
}
You can suppress specific warnings by passing a string argument like: #SuppressWarnings("unchecked"). See What is the list of valid #SuppressWarnings warning names in Java? For a list.

Android.App.Activity - package does not exist

I got an andorid project files that are supposed to compile correctly.
I installed the latest eclipse and Andriod ADT plugins.
I then imported the project and right clicked build.xml to run as ANT build. but I got erors on some basic code which I am sure is originating from my misconfigured setup.
I also tried to run it using ANT from the command line. both times I got the same type of errors
What is wrong ?
Buildfile: /Users/admin/Downloads/moshe-5/build.xml
init:
process.annotations:
[javac] Compiling 9 source files to /Users/admin/Downloads/moshe-5/build/classes
[javac] /Users/admin/Downloads/moshe-5/src/ti/moshe/CustomAdapter.java:7: package android.app does not exist
[javac] import android.app.Activity;
[javac] ^
[javac] /Users/admin/Downloads/moshe-5/src/ti/moshe/CustomAdapter.java:8: package android.content does not exist
[javac] import android.content.Context;
[javac] ^
[javac] /Users/admin/Downloads/moshe-5/src/ti/moshe/CustomAdapter.java:9: package android.graphics does not exist
[javac] import android.graphics.Color;
In my case, it was due to the problem that I didn't have a default.properties file with the target field in it.
I had to manually create the file and put
target=android-9
Or other Android target version.
If you run
ant -v
with your build command, you'll probably see this line
Property "target" has not been set
and
[property] Unable to find property file: /PATH_TO/default.properties
Those messages are enough hints.
Hope this solves your problem too.
It is not finding the android packages. IN the build step you should include android.jar corresponding to the android version you want to port to.
When you write your compile target, you are overriding the default one given by android_rules.xml located in: C:\Program Files (x86)\Android\android-sdk\platforms\android-8\templates or wherever android_rules.xml is located on your computer.
Like Potter mentioned above it is not finding the android library and other libraries, so please look at android_rules.xml to see how it sets up the proper libraries:
<target name="compile" depends="-resource-src, -aidl"
description="Compiles project's .java files into .class files">
<!-- If android rules are used for a test project, its classpath should include
tested project's location -->
<condition property="extensible.classpath"
value="${tested.project.absolute.dir}/bin/classes" else=".">
<isset property="tested.project.absolute.dir" />
</condition>
<condition property="extensible.libs.classpath"
value="${tested.project.absolute.dir}/libs" else="./libs">
<isset property="tested.project.absolute.dir" />
</condition>
<javac encoding="ascii" target="1.5" debug="true" extdirs=""
destdir="${out.classes.absolute.dir}"
bootclasspathref="android.target.classpath"
verbose="${verbose}" classpath="${extensible.classpath}">
<src path="${source.absolute.dir}" />
<src path="${gen.absolute.dir}" />
<classpath>
<fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
<fileset dir="${extensible.libs.classpath}" includes="*.jar" />
</classpath>
</javac>
</target>
Inside the classpath tags is where android.jar is included. You can add other libraries by adding more filesets
Some other good examples of code for writing the ANT compile target are:
Can't build and run an android test project created using "ant create test-project" when tested project has jars in libs directory <-- this worked for me
http://www.vogella.de/articles/ApacheAnt/article.html
http://www.alittlemadness.com/2010/05/31/setting-up-an-android-project-build/ <-- setting up ANT project
It's most likely not finding Android because you didn't define the sdk.dir property, which tells the build process where to find Android. This property is usually in a local.properties file and is populated when you do:
android update project
as described here:
http://developer.android.com/tools/projects/projects-cmdline.html#UpdatingAProject
Don't bother building using ANT.
Eclipse and the ADT plugin provide excellent tool to save you the trouble.
Simply right click the project, choose run as -> Android application

Why javac task does not creates classes

I have an ant task that contains javac task inside. It reports about error in one of the classes, but the build doesn't fail, because it has failonerror="false". I suppose to see class files in the end of build, but I don't see it. Can anybody tell me why?
Properties used:
checkout.path=work/workingcopy
classpath.path=work/build/classes
log.file=work/log.txt
Ant code:
<record name="${log.file}" action="start"/>
<javac destdir="${classpath.path}" srcdir="${checkout.path}/src"
debug="true" failonerror="false">
<classpath>
<path refid="webinf.lib"/>
<path refid="tomcat.lib"/>
</classpath>
</javac>
<record name="${log.file}" action="stop"/>
Log file:
[javac] Compiling 169 source files to C:\work\build\classes
[javac] C:\work\workingcopy\src\com\mycompany\exception\handlerException\CustomExceptionHandler.java:25: cannot find symbol
[javac] symbol : class RequestContextImpl
[javac] location: package org.primefaces.context
[javac] import org.primefaces.context.RequestContextImpl;
[javac] ^
[javac] C:\work\workingcopy\src\com\mycompany\exception\handlerException\CustomExceptionHandler.java:103: cannot find symbol
[javac] symbol : class RequestContextImpl
[javac] location: class com.mycompany.exception.handlerException.CustomExceptionHandler
[javac] new RequestContextImpl(ec);
[javac] ^
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 2 errors
[javac] Compile failed; see the compiler error output for details.
The failonerror option is for Ant not for javac. So if failonerror=false then Ant will continue your task even javac returns an error.
From the docs:
failonerror Indicates whether compilation errors will fail the build; defaults to true.
The build is Ant's build process not javac's !
Use Eclipse Java Compiler (EJC) instead of standard Oracle javac. One of ECJ advantages over javac is that it is permissible of errors, it tries to compile as much as possible and keeps already generated class files. With javac it is everything or nothing.
EJC was developed for use in IDE, for highly interactive work where partial compilation is a must, but it can also be used as CLI or Maven plugin. Plexus guys provide EJC as a handy Maven dependency.
For example configuration of ECJ in Maven, a partial compilation, check my answer to this question:
Maven: partial compilation before code generation
I've had same problem. And that is soluion I've found on the Web
This depends on the compiler that you are actually using. If you're using
javac 1.3, this is what Sun does by design. (see
http://java.sun.com/j2se/1.3/docs/tooldocs/tools-changes.html for more
information, specifically the last paragraph of the first bullet.)
Try using an older compiler or using the jikes compiler from IBM. Both
javac 1.2.2 and jikes 1.13 will produce output for classes that do compile,
even if another file in the fileset doesn't.
Jay
Best regards,
Michael

Categories