简体   繁体   中英

jar and cannot find symbol for method (but other class methods are found)?

I really have problem with this one.

So I have a jar with a class for handling a database connection. In that class I have put several methods. Out of this class I have created a jar using Ant.

Sometime yesterday I suddenly could not access the latest method in that db class anymore. I get:

compile:
[javac] C:\cygwin\home\user\Dev\Java\avc_alarm\build.xml:22: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to C:\cygwin\home\user\Dev\Java\avc_alarm\build
[javac] C:\cygwin\home\user\Dev\Java\avc_alarm\src\se\gefa\avc_alarm\Main.java:9: error: cannot find symbol
[javac] symbolAliases = db.getAlarmSymbolAlias();
[javac]                   ^
[javac]   symbol:   method getAlarmSymbolAlias()
[javac]   location: variable db of type AvcDb
[javac] 1 error

Code that tries to use this class method:

package se.gefa.avc_alarm;

import se.gefa.avc_db.AvcDb;
import java.util.ArrayList;

public class Main {
    static AvcDb db  = new AvcDb();
    static ArrayList<String> symbolAliases;

    public static void main(String args[]) throws Exception {
        db.setupConnection("jdbc:h2:tcp://localhost/C:/XXXX/xyz", "sa", "");
        symbolAliases = db.getAlarmSymbolAlias();
    }
}

And the db class (which goes into a jar):

package se.gefa.avc_db;

import java.sql.*;
import java.util.ArrayList;

public class AvcDb {

    // ---
    // Other methods that DO work
    // ---

    // The one that DO NOT work
    public ArrayList<String> getAlarmSymbolAlias() throws Exception {
        String query = "SELECT * FROM Alarm";
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(query);

        ArrayList<String> symbols = new ArrayList<String>();
        while (resultSet.next())
            symbols.add(resultSet.getString("symbol_alias"));

        return symbols;
    }
}

I'm using java 7, Ant 1.8.3 and Vim. When opening the db jar in IntelliJ I can see that the getAlarmSymbolAlias method exists. And as I said, I could call any other of the methods for the db class except any method added yesterday. A simple test in IntelliJ and it seems to work (but I don't want to learn a new Editor when I got all other set up in Vim, CL and Ant.

Edit Ant script:

<project name="AvcAlarm" default="dist" basedir=".">
  <description>
    Alarm program. 
  </description>

  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>
  <property name="lib" location="lib"/>

  <path id="classpath">
    <fileset dir="${lib}" includes="**/*.jar"/>
  </path>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
    <mkdir dir="${dist}"/>
  </target>

  <target name="compile" depends="init" description="compile the source " >
    <javac srcdir="${src}" destdir="${build}" classpathref="classpath"/>
  </target>

  <target name="dist" depends="compile" description="generate the distribution" >
    <jar destfile="${dist}/gefa_alarm.jar" basedir="${build}">
        <zipgroupfileset dir="lib" includes="*.jar" />
        <manifest>
            <attribute name="Main-Class" value="se/gefa/avc_alarm/Main"/>
        </manifest>
    </jar>
  </target>

  <target name="clean" description="clean up" >
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Your classpath must be wrong, or you use an invalid version of the JAR that doesn't contain this method.

Updated after investigation with OP : another JAR in the lib folder contained a different version of the AvcDb, and was being loaded first on the classpath, so the desired method could not be found.

Since you have verified your JAR contains the newly added method, there should be a problem in the class path and some other older version of the class is loaded. Change the order of the class path in order to set this jar file at the top.

I suspect that your JAR file is not on your classpath when you are compiling AvcDb.java, OR you have an old version of yourt JAR on your classpath with a different method signature.

You should also delete all old *.class files that may have been created from a previous build. Do you have a "prepare" target in your ANT build.xml that removes all old build artifacts prior to compiling again?

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