javac compilation issue with packages - java

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.

Related

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 :-)

Javac compiler bug with class name antbuild

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.

Ant compiles but doesn't create the classes

Ok, sorry if it is repeated, but I promise I googled it, searched around forums and similar.
The thing is, I have to compile a big java project to create a war file. The problem is, it depends on a lot of classes and jars that I own.
So, I started building the project from command line, everything goes right, but when I tried to apply the same with ant, ant doesn't create the classes... or directories... or anything. It says that the compilation was successful but the output directory does not have any classes.
So, here's part of my partial build.xml, at least, the important part for me.
<target name="we">
<javac destdir="${proj.name}/${comp.res}">
<src path="${proj.name}/cnt.web/${proj.version}/exception" />
<src path="${proj.name}/cnt.web/${proj.version}/base" />
<src path="${proj.name}/cnt.web/${proj.version}/core" />
<classpath refid="my.path"/>
</javac>
</target>
I've tried with lots of combinations, from srcdir to this aproach. Nothing seems to work when I do:
ant we
It says that it's successful, that it registered n files to compile, and that's all, the resulting directories and classes aren't where they are suposed to be. I tried reading the debug and verbose but nothing seems to be wrong.
Any ideas could help.

Configure specific package for generated classes in ADT / ant build

I'm developing an application for various customers that use exactly the same code (it just displays a WebView). The only differences are one config file, the icon and the android package name, all generated via a custom ant task.
If I run the ant release target or build the project in eclipse, the generated Java classes (like R) are generated in a package named like the android package. As a result, imports of these classes in my project are invalid, because their package has changed.
Newsnet.java
package ch.newsnet.app;
import ch.newsnet.app.R; // <- cannot be found after build
public class Newsnet extends FragmentActivity {
//...
}
Running my custom ant target will set my android package to ch.customer1.app, wich makes R a class of package ch.customer1.app. But I want it always to be ch.newsnet.app. How can I achieve that in a) the ant build and b) in the eclipse builder? I don' want to setup 20+ projects and maintain their buildfile and resources when I can have all in one code base.
For those who face the same issue, I solved this problem as follows (allthough it's not the best solution, but it worked for me):
I created the ant target moveGeneratedFiles, where I copy the generated package to the correct one, replace the package name and delete the generated one (which will not really work in eclipse because the classes will be generated again).
<target name="moveGeneratedFiles">
<loadproperties>
<file file="assets/customer.properties"/>
</loadproperties>
<copy todir="gen/ch/newsnet/app" overwrite="true">
<fileset dir="gen/ch/${customer.name}/app" />
</copy>
<replace dir="gen/ch/newsnet/app" token="package ch.${customer.name}.app;" value="package ch.newsnet.app;" />
<delete>
<fileset dir="gen/ch/${customer.name}/app" />
</delete>
</target>
In the project properties, I added an ant builder executing this target so the generated classes will be synced after saving.

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

Categories