代码之家  ›  专栏  ›  技术社区  ›  Justian Meyer

Jar找不到连接到主类的主方法,但无法访问方法

  •  2
  • Justian Meyer  · 技术社区  · 14 年前

    我一直在Eclipse中从事一个项目,除了一些Ant问题外,其他一切都进展顺利。

    我的代码编译得非常完美,完全符合我的要求,但是,当它在jar中时,我得到了错误:

    Exception in thread "main" java.lang.NoSuchMethodError: main
    

    Main肯定存在于文件中,所以我猜它与文件的构造方式有关。我一直在使用SuperCSV阅读一些excel表格,并围绕网站上的示例构建了我的主类: http://supercsv.sourceforge.net/codeExamples_general.html

    package jab.jm.main;
    
    import jab.jm.readers.FileManager;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    
    import junit.framework.Test;
    
    import org.supercsv.cellprocessor.constraint.StrMinMax;
    import org.supercsv.cellprocessor.ift.CellProcessor;
    import org.supercsv.io.CsvBeanReader;
    import org.supercsv.io.ICsvBeanReader;
    import org.supercsv.prefs.CsvPreference;
    
    class ReadingObjects {
    
        static final CellProcessor[] userProcessors = { null,
                new StrMinMax(0, 100), null, new StrMinMax(0, 100), null,
                new StrMinMax(0, 100), null, null, null, null, null, null, null,
                null, null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, null, new StrMinMax(0, 100), null,
                new StrMinMax(0, 100), null, null, null, null, null, null, null,
                null, null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, null, null, null, null, null };
    
    
        public static void main(String[] args) throws Exception {
            ICsvBeanReader inFile = new CsvBeanReader(
                    new FileReader(
                            "C:\\Users\\Justian\\workspaces\\LNConnectionCompiler\\src\\jab\\jm\\main\\Fred_Jewell.csv"),
                    CsvPreference.EXCEL_PREFERENCE);
            try {
                final String[] header = { "title", "firstName", "middleName",
                        "lastName", "suffix", "email", "email2", "email3",
                        "businessStreet", "businessStreet2", "businessStreet3",
                        "businessCity", "businessState", "businessPostalCode",
                        "businessCountry", "homeStreet", "homeStreet2",
                        "homeStreet3", "homeCity", "homeState", "homePostalCode",
                        "homeCountry", "otherStreet", "otherStreet2",
                        "otherStreet3", "otherCity", "otherState",
                        "otherPostalCode", "otherCountry", "company", "department",
                        "jobTitle", "assistantPhone", "businessFax",
                        "businessPhone", "businessPhone2", "callback", "carPhone",
                        "companyMainPhone", "homeFax", "homePhone", "homePhone2",
                        "isdn", "mobilePhone", "otherFax", "otherPhone", "pager",
                        "primaryPhone", "radioPhone", "TTYTTDPhone", "telex",
                        "assistantName", "birthday", "managerName", "notes",
                        "otherPOBox", "spouse", "webPage", "personalWebPage" };
                ExcelFile file;
                while ((file = inFile.read(ExcelFile.class, header, userProcessors)) != null) {
                    if (file.getCompany().indexOf("Jabian") >= 0)
                        System.out.println(file.getFirstName() + " " + file.getLastName());
                }
            } finally {
                inFile.close();
            }
    
            URL location = ExcelFile.class.getProtectionDomain().getCodeSource().getLocation();
            System.out.println(location.getFile());
    
        }
    }
    
    public class ExcelFile {
        private String title, firstName, middleName, lastName, suffix, email,
                email2, email3, businessStreet, businessStreet2, businessStreet3,
                businessCity, businessState, businessPostalCode, businessCountry,
                homeStreet, homeStreet2, homeStreet3, homeCity, homeState,
                homePostalCode, homeCountry, otherStreet, otherStreet2,
                otherStreet3, otherCity, otherState, otherPostalCode, otherCountry,
                company, department, jobTitle, assistantPhone, businessFax,
                businessPhone, businessPhone2, callback, carPhone,
                companyMainPhone, homeFax, homePhone, homePhone2, isdn,
                mobilePhone, otherFax, otherPhone, pager, primaryPhone, radioPhone,
                TTYTTDPhone, telex, assistantName, birthday, managerName, notes,
                otherPOBox, spouse, webPage, personalWebPage;
    
        public String getTitle() {
            return title;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public String getMiddleName() {
            return middleName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public String getSuffix() {
            return suffix;
        }
    
        public String getEmail() {
            return email;
        }
    
        public String getEmail2() {
            return email2;
        }
    
        public String getEmail3() {
            return email3;
        }
    
        public String getBusinessStreet() {
            return businessStreet;
        }
    
        public String getBusinessStreet2() {
            return businessStreet2;
        }
    
        public String getBusinessStreet3() {
            return businessStreet3;
        }
    
        public String getBusinessCity() {
            return businessCity;
        }
    
        public String getBusinessState() {
            return businessState;
        }
    
        public String getBusinessPostalCode() {
            return businessPostalCode;
        }
    
        public String getBusinessCountry() {
            return businessCountry;
        }
    
        public String getHomeStreet() {
            return homeStreet;
        }
    
        public String getHomeStreet2() {
            return homeStreet2;
        }
    
        public String getHomeStreet3() {
            return homeStreet3;
        }
    
        public String getHomeCity() {
            return homeCity;
        }
    
        public String getHomeState() {
            return homeState;
        }
    
        public String getHomePostalCode() {
            return homePostalCode;
        }
    
        public String getHomeCountry() {
            return homeCountry;
        }
    
        public String getOtherStreet() {
            return otherStreet;
        }
    
        public String getOtherStreet2() {
            return otherStreet2;
        }
    
        public String getOtherStreet3() {
            return otherStreet3;
        }
    
        public String getOtherCity() {
            return otherCity;
        }
    
        public String getOtherState() {
            return otherState;
        }
    
        public String getOtherPostalCode() {
            return otherPostalCode;
        }
    
        public String getOtherCountry() {
            return otherCountry;
        }
    
        public String getCompany() {
            return company;
        }
    
        public String getDepartment() {
            return department;
        }
    
        public String getJobTitle() {
            return jobTitle;
        }
    
        public String getAssistantPhone() {
            return assistantPhone;
        }
    
        public String getBusinessFax() {
            return businessFax;
        }
    
        public String getBusinessPhone() {
            return businessPhone;
        }
    
        public String getBusinessPhone2() {
            return businessPhone2;
        }
    
        public String getCallBack() {
            return callback;
        }
    
        public String getCarPhone() {
            return carPhone;
        }
    
        public String getCompanyMainPhone() {
            return companyMainPhone;
        }
    
        public String getHomeFax() {
            return homeFax;
        }
    
        public String getHomePhone() {
            return homePhone;
        }
    
        public String getHomePhone2() {
            return homePhone2;
        }
    
        public String getISDN() {
            return isdn;
        }
    
        public String getMobilePhone() {
            return mobilePhone;
        }
    
        public String getOtherFax() {
            return otherFax;
        }
    
        public String getOtherPhone() {
            return otherPhone;
        }
    
        public String getPager() {
            return pager;
        }
    
        public String getPrimaryPhone() {
            return primaryPhone;
        }
    
        public String getRadioPhone() {
            return radioPhone;
        }
    
        public String getTTYTTDPhone() {
            return TTYTTDPhone;
        }
    
        public String getTelex() {
            return telex;
        }
    
        public String getAssistantName() {
            return assistantName;
        }
    
        public String getBirthday() {
            return birthday;
        }
    
        public String getManagerName() {
            return managerName;
        }
    
        public String getNotes() {
            return notes;
        }
    
        public String getOtherPOBox() {
            return otherPOBox;
        }
    
        public String getSpouse() {
            return spouse;
        }
    
        public String getWebPage() {
            return webPage;
        }
    
        public String getPersonalWebPage() {
            return personalWebPage;
        }
    
        public void setTitle(final String title) {
            this.title = title;
        }
    
        public void setFirstName(final String firstName) {
            this.firstName = firstName;
        }
    
        public void setMiddleName(final String middleName) {
            this.middleName = middleName;
        }
    
        public void setLastName(final String lastName) {
            this.lastName = lastName;
        }
    
        public void setSuffix(final String suffix) {
            this.suffix = suffix;
        }
    
        public void setEmail(final String email) {
            this.email = email;
        }
    
        public void setEmail2(final String email2) {
            this.email2 = email2;
        }
    
        public void setEmail3(final String email3) {
            this.email3 = email3;
        }
    
        public void setBusinessStreet(final String businessStreet) {
            this.businessStreet = businessStreet;
        }
    
        public void setBusinessStreet2(final String businessStreet2) {
            this.businessStreet2 = businessStreet2;
        }
    
        public void setBusinessStreet3(final String businessStreet3) {
            this.businessStreet3 = businessStreet3;
        }
    
        public void setBusinessCity(final String businessCity) {
            this.businessCity = businessCity;
        }
    
        public void setBusinessState(final String businessState) {
            this.businessState = businessState;
        }
    
        public void setBusinessPostalCode(final String businessPostalCode) {
            this.businessPostalCode = businessPostalCode;
        }
    
        public void setBusinessCountry(final String businessCountry) {
            this.businessCountry = businessCountry;
        }
    
        public void setHomeStreet(final String homeStreet) {
            this.homeStreet = homeStreet;
        }
    
        public void setHomeStreet2(final String homeStreet2) {
            this.homeStreet2 = homeStreet2;
        }
    
        public void setHomeStreet3(final String homeStreet3) {
            this.homeStreet3 = homeStreet3;
        }
    
        public void setHomeCity(final String homeCity) {
            this.homeCity = homeCity;
        }
    
        public void setHomeState(final String homeState) {
            this.homeState = homeState;
        }
    
        public void setHomePostalCode(final String homePostalCode) {
            this.homePostalCode = homePostalCode;
        }
    
        public void setHomeCountry(final String homeCountry) {
            this.homeCountry = homeCountry;
        }
    
        public void setOtherStreet(final String otherStreet) {
            this.otherStreet = otherStreet;
        }
    
        public void setOtherStreet2(final String otherStreet2) {
            this.otherStreet2 = otherStreet2;
        }
    
        public void setOtherStreet3(final String otherStreet3) {
            this.otherStreet3 = otherStreet3;
        }
    
        public void setOtherCity(final String otherCity) {
            this.otherCity = otherCity;
        }
    
        public void setOtherState(final String otherState) {
            this.otherState = otherState;
        }
    
        public void setOtherPostalCode(final String otherPostalCode) {
            this.otherPostalCode = otherPostalCode;
        }
    
        public void setOtherCountry(final String otherCountry) {
            this.otherCountry = otherCountry;
        }
    
        public void setCompany(final String company) {
            this.company = company;
        }
    
        public void setDepartment(final String department) {
            this.department = department;
        }
    
        public void setJobTitle(final String jobTitle) {
            this.jobTitle = jobTitle;
        }
    
        public void setAssistantPhone(final String assistantPhone) {
            this.assistantPhone = assistantPhone;
        }
    
        public void setBusinessFax(final String businessFax) {
            this.businessFax = businessFax;
        }
    
        public void setBusinessPhone(final String businessPhone) {
            this.businessPhone = businessPhone;
        }
    
        public void setBusinessPhone2(final String businessPhone2) {
            this.businessPhone2 = businessPhone2;
        }
    
        public void setCallback(final String callback) {
            this.callback = callback;
        }
    
        public void setCarPhone(final String carPhone) {
            this.carPhone = carPhone;
        }
    
        public void setCompanyMainPhone(final String companyMainPhone) {
            this.companyMainPhone = companyMainPhone;
        }
    
        public void setHomeFax(final String homeFax) {
            this.homeFax = homeFax;
        }
    
        public void setHomePhone(final String homePhone) {
            this.homePhone = homePhone;
        }
    
        public void setHomePhone2(final String homePhone2) {
            this.homePhone2 = homePhone2;
        }
    
        public void setIsdn(final String isdn) {
            this.isdn = isdn;
        }
    
        public void setMobilePhone(final String mobilePhone) {
            this.mobilePhone = mobilePhone;
        }
    
        public void setOtherFax(final String otherFax) {
            this.otherFax = otherFax;
        }
    
        public void setOtherPhone(final String otherPhone) {
            this.otherPhone = otherPhone;
        }
    
        public void setPager(final String pager) {
            this.pager = pager;
        }
    
        public void setPrimaryPhone(final String primaryPhone) {
            this.primaryPhone = primaryPhone;
        }
    
        public void setRadioPhone(final String radioPhone) {
            this.radioPhone = radioPhone;
        }
    
        public void setTTYTTDPhone(final String TTYTTDPhone) {
            this.TTYTTDPhone = TTYTTDPhone;
        }
    
        public void setTelex(final String telex) {
            this.telex = telex;
        }
    
        public void setAssistantName(final String assistantName) {
            this.assistantName = assistantName;
        }
    
        public void setBirthday(final String birthday) {
            this.birthday = birthday;
        }
    
        public void setManagerName(final String managerName) {
            this.managerName = managerName;
        }
    
        public void setNotes(final String notes) {
            this.notes = notes;
        }
    
        public void setOtherPOBox(final String otherPOBox) {
            this.otherPOBox = otherPOBox;
        }
    
        public void setSpouse(final String spouse) {
            this.spouse = spouse;
        }
    
        public void setWebPage(final String webPage) {
            this.webPage = webPage;
        }
    
        public void setPersonalWebPage(final String personalWebPage) {
            this.personalWebPage = personalWebPage;
        }
    
    }
    

    在我看来,这是一个糟糕的结构,但它是完全可能的,这就是为什么我没有遇到任何问题。当然,因为这是一个如此不寻常的结构,Ant可能在尝试访问它时遇到了麻烦。

    注意,ExcelFile.Java包含非公共类ReadingObjects,它包含main方法。我的理论是,因为ExcelFile.java不一定是包含main方法的类,所以我遇到了一些问题

    非常感谢!

    编辑:这是我的生成文件:

    <?xml version="1.0" ?>
    
    <project name="ServerJar" default="dist" basedir=".">
        <description>
            Builds client files into .jar
        </description>
        <!-- [build variables] -->
        <property name="src" location="src" />
        <property name="build" location="build" />
        <property name="dist" location="dist" />
        <property name="lib" location="lib" />
        <!-- [path to packages] -->
        <path id="master-classpath">
            <fileset dir="${lib}">
                <include name="*.jar"/>
            </fileset>
        </path>
    
    
        <target name="init">
            <!-- makes time stamp to be used in jar name -->
            <tstamp />
            <!-- creates build directory structure -->
            <mkdir dir="${build}" />
        </target>
    
        <target name="compile" depends="init" description="Compiles the source">
            <!-- compiles the java code from ${src} into ${build} -->
            <!-- <javac srcdir="${src}" destdir="${build}" /> -->
            <javac destdir= "${build}">
                <src path="${src}"/>
                <classpath refid="master-classpath"/>
            </javac>
        </target>
    
        <target name="dist" depends="compile" description="Generates distributable">
        <!-- creates the distribution directory -->
            <mkdir dir="${dist}/lib" />
    
            <!-- puts everything in ${build} into the jar file -->
            <jar jarfile="${dist}/lib/CC-${DSTAMP}.jar" basedir="${build}">
                <manifest>
                    <attribute name="Main-Class" value="jab.jm.main.ExcelFile" />
                </manifest>
            </jar>
    
            <!-- makes a jar file for quick test execution -->
            <jar jarfile="${dist}/lib/CC.jar" basedir="${build}">
                <manifest>
                    <attribute name="Main-Class" value="jab.jm.main.ExcelFile" />
                </manifest>
            </jar>
        </target>
    
        <target name="clean" description="Cleans up the extra build files">
            <!-- deletes the ${build} and ${dist} directories -->
            <delete dir="${build}" />
            <delete dir="${dist}" />
        </target>
    </project>
    

    5 回复  |  直到 14 年前
        1
  •  1
  •   BalusC    14 年前

    这个 -cp (和 -classpath )论点是 使用时 -jar 争论。您需要在 Class-Path 罐子的属性 MANIFEST.MF

    我的蚂蚁有点生锈了,但它有点像那些线条:

    <attribute name="Class-Path" value="${dist.classpath}"/>
    

    根据您的评论,您想知道如何直接用Eclipse而不是用Ant来实现这一点。首先,您需要运行 main() 方法使用 Ctrl键 11层 导出>可运行JAR文件 启动配置 从列表中(如有必要,请选择“库处理”),然后单击 完成 . 这样,Eclipse在生成 MANIFEST 文件。

        2
  •  2
  •   Pablo Santa Cruz    14 年前

    Main-Class: com.yourcompany.ReadingObjects
    

    之后,您需要像这样运行程序:

    $ java -jar nameofyourjar.jar
    

    $ java -cp nameofyourjar.jar com.yourcompany.ReadingObjects
    

    当做。

        3
  •  0
  •   Donal Fellows    14 年前

    不管你在建什么,最终都会有一个 META-INF/MANIFEST.MF 在你的罐子里。那个文件需要有一个 Main-Class 必须 兼而有之 public static .

    另一个,重要的,明白了。您需要将所有依赖项打包到可执行JAR文件中,因为您不能将JAR之外的内容放在类路径上(你可以使用另一个类加载器和动态加载技巧,但这是一个完全不同的层次的毛茸茸的东西。)当你开始进入这种事情,Maven开始看起来有点不那么势不可挡

        4
  •  0
  •   user177800 user177800    14 年前

    main 方法实际上在 ReadingObjects public 也。

    Main-Class: package jab.jm.main.ReadingObjects
    

    这里有详细的说明和一个工作的ANT文件来做你想做的事情。 Build an Executable .jar file

    在再次研究代码之后,我认为应该调用源文件 ReadingObjects.java , 读取对象

        5
  •  0
  •   some_id    14 年前

    您也可以在eclipse中创建一个可运行的jar而不是Ant构建,它会询问您哪个文件包含main,它通常会找到正确的文件。如果我没记错的话,您应该选择将内容打包到.jar文件中。