简体   繁体   中英

ant build gives package does not exist error

I am using ant build function on linux to compile a java program. I have three java files and I'm trying to import them to my main.java file but I get an error saying

/home/ggaz1/Ser321/Assigns/Assign2/src/main.java:4 error: package Assign2.src does not exist 

here is the build.xml file. the path to it is

/home/ggaz1/Ser321/Assigns/Assign2

<?xml version="1.0"?>
<project name="Movie Library."
         default="targets" basedir="."
         xmlns:dn="antlib:org.apache.ant.dotnet"
         xmlns="antlib:org.apache.tools.ant"
         xmlns:cpptasks="antlib:net.sf.antcontrib.cpptasks">

   <property name="src.dir" value="src"/>
   <property name="build" value="classes"/>
   <property environment="env"/>
   <property name="user" value="${env.USERNAME}"/>

   <target name="targets">
      <echo message="Targets are clean, prepare, build, execute, and targets"/>
   </target>

   <path id="compile.classpath">
      <pathelement location="${build}"/>
   </path>

   <target name="prepare">
      <mkdir dir="${build}" />
   </target>

   <target name="clean">
      <delete dir="${build}" failonerror="false"/>
   </target>

   <target name="build" depends="prepare">
      <javac srcdir="${src.dir}"
             includeantruntime="false"
             destdir="${build}">
        <src path="src"/>
         <classpath refid="compile.classpath"/>
      </javac>
   </target>

   <target name="execute.java" depends="build"
           description="Run the program">
         <echo message="command line execute: java -cp classes main, MovieDescription, MovieLibrary" />
         <java classname="main" fork="yes">
           <classpath refid="compile.classpath"/>
         </java>
   </target>
</project>

thanks in advance

You actually don't have any packages. All your Java source code files are located in the src folder. The src folder is the root of your packages. If you want a package assign2 you have to create a folder in src called assign2 and you should place all Java files which belong to the assign2 package into that folder.

Assuming your Main class itself is without a package and your classes MovieDescription and MovieLibrary are in package assign2 .

Then your file layout should look like:

--src
   |
   +---- Main.java
   |
   +---- assign2
            |
            +---- MovieDescription.java
            |
            +---- MovieLibrary.java

Then Main.java doesn't have any package declarartion.

Im MovieDescription.java and MovieLibrary.java the package declaration should be:

package assign2;
[...]

And the imports in Main.java look like:

import assign2.MovieLibrary;
import assign2.MovieDescription;

Maybe you should have a look at the Oracle Java Tutorial, Lesson: Packages .

And according to the Java naming conventions you should use only names starting with lower case letters for packages and variables. Classes should start always with an upper case letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM