Java 我的Jar使用JDK7启动,但不使用JDK1.8.0_101。为什么不呢?错误:无法找到或加载主类

Java 我的Jar使用JDK7启动,但不使用JDK1.8.0_101。为什么不呢?错误:无法找到或加载主类,java,maven,jar,java-8,java-7,Java,Maven,Jar,Java 8,Java 7,所以,我试图启动一个我已经构建的jar应用程序,但我遇到了一个非常奇怪的问题。当我试图使用java-jar mycokie.jar在命令行上启动应用程序时,我收到了以下错误:错误:找不到或加载主类 现在我知道这是一个常见的错误。它基本上告诉我JVM找不到主类(或者主类有问题)。当然,我检查的第一件事是: A) 我是否使用了正确的FQCN,并正确拼写了它?对 B) 我是否在CMD中使用了mycokie.jar而不是简单的mycokie,在清单中使用了main.java.mycokie.mainCl

所以,我试图启动一个我已经构建的jar应用程序,但我遇到了一个非常奇怪的问题。当我试图使用
java-jar mycokie.jar
在命令行上启动应用程序时,我收到了以下错误:错误:找不到或加载主类

现在我知道这是一个常见的错误。它基本上告诉我JVM找不到主类(或者主类有问题)。当然,我检查的第一件事是:

A) 我是否使用了正确的FQCN,并正确拼写了它?对

B) 我是否在CMD中使用了mycokie.jar而不是简单的mycokie,在清单中使用了main.java.mycokie.mainClass而不是main.java.mycokie.mainClass.class?对

C) 所需的类实际上是位于JAR中还是位于类路径中?是的,该类位于main.java.mycokie包中,位于mycokie.jar中

在检查了这些东西之后,我怀疑Java可能出了问题,特别是我的机器上安装了4种不同的Java(jre7、jre1.8.0_101、jdk1.7.0_80、jdk1.8.0_101),因此我尝试在命令行上用不同的版本运行可执行jar应用程序

非常有趣的是,我发现1.8.0 jre和JDK都产生了前面提到的错误,但是应用程序使用jre7和jdk1.7.080正确执行。毫不奇怪,
java-jar mycokie.jar
无法工作,因为我的“java”被设置为jdk1.8.0

这可能不太可能,但有人知道为什么我的应用程序可以在JSE7中工作,但会触发在1.8.0中加载主类的失败吗

我使用Maven作为构建工具,我的主要类是mainView,如下所示:

package main.java.myCookie;

import main.java.gls.entities.Emp;
import main.java.gls.entities.JobStatus;
import main.java.gls.event.StringEventListener;
import main.java.gls.util.JustOneLock;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.swing.JComponent;
import main.java.myCookie.background.fetchEmpList;
import main.java.myCookie.background.fetchStatusList;
import main.java.myCookie.interfaces.Main;


    public class mainView
            extends javax.swing.JFrame
            implements Main
    {
        @PersistenceUnit
        final private EntityManagerFactory emFactory = javax.persistence.Persistence.createEntityManagerFactory("JobEntitiesPU");

        @PersistenceContext
        final private EntityManager em = emFactory.createEntityManager();

        Properties config = new Properties();

        ArrayList<EventListener> listenerList = new ArrayList<EventListener>();

        //set from property file in working directory
        String defaultPrinter;
        ArrayList<Integer> statusExceptionList = new ArrayList<Integer>();
        ArrayList<Integer> statusLockedList = new ArrayList<Integer>();

        ArrayList<JobStatus> adjustedStatusList = new ArrayList<JobStatus>();

        ArrayList<JobStatus> statusList = new ArrayList<JobStatus>();
        boolean statusListFetched = false;

        int defaultEmpId;
        Emp defaultEmp;
        Emp noEmp;
        ArrayList<Emp> empList = new ArrayList<Emp>();
        boolean empListFetched = false;

        PanelManager panelManager;

        public mainView()
        {
            lockTest();
            init();
        }

        private void lockTest()
        {
            JustOneLock jol = new JustOneLock("TimeClock");

            if ( jol.isAppActive() )
            {
                System.out.println("Instance of TimeClock is already Running");
                System.exit(1);
            }
        }

        private void init()
        {
            MainSingleton.getInstance().setMain(this);

            this.openPropertiesFromFile();

            this.fetchEmpList();
            this.fetchStatusList();

            initComponents();

            panelManager = new PanelManager();

            this.setVisible(true);
        }

        private void openPropertiesFromFile()
        {
            System.out.println( "Current Working Directory: "+System.getProperty("user.dir"));

            //get properties file
            try
            {
                this.config.load(new FileInputStream( System.getProperty("user.dir")+"\\main.java.myCookie.properties"));

            } catch (IOException ex)
            {
                Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
            }

            //get default user if it exists
            if ( config.containsKey("DEFAULT_USER") )
            {
                System.out.println( "DEFAULT_USER: "+config.getProperty("DEFAULT_USER") );

                this.defaultEmpId = Integer.parseInt( config.getProperty("DEFAULT_USER") );
            }
            else
            {
                System.out.println( "DEFAULT_USER: NOT FOUND (set to 1000)" );

                this.defaultEmpId = 1000;
            }

            //get default printer if it exists
            if ( config.containsKey("DEFAULT_PRINTER") )
            {
                System.out.println( "DEFAULT_PRINTER: "+config.getProperty("DEFAULT_PRINTER") );

                this.defaultPrinter =  config.getProperty("DEFAULT_PRINTER");
            }
            else
            {
                System.out.println( "DEFAULT_PRINTER: NOT FOUND" );

                this.defaultPrinter = "myPrinter";
            }

            //get status exceptions if it exists ( comma delineated )
            if ( config.containsKey("STATUS_EXCEPTIONS") )
            {
                System.out.println( "STATUS_EXCEPTIONS list found" );

                String[] rawSE = config.getProperty("STATUS_EXCEPTIONS").split(",");

                for ( String s : rawSE )
                {                
                    this.statusExceptionList.add(Integer.parseInt(s));
                }
            }
            else
            {
                System.out.println( "STATUS_EXCEPTIONS list NOT found" );
            }

            //get locked status list if it exists ( coma delineated )
            if ( config.containsKey("STATUS_LOCKED") )
            {
                System.out.println( "STATUS_LOCKED list found" );

                String[] rawSL = config.getProperty("STATUS_LOCKED").split(",");

                for ( String s : rawSL )
                {
                    this.statusLockedList.add(Integer.parseInt(s));
                }
            }
            else
            {
                System.out.println( "STATUS_LOCKED list NOT found" );
            }
        }

        private void fetchEmpList()
        {
            final fetchEmpList task = new fetchEmpList();
            task.addPropertyChangeListener
            (
                new PropertyChangeListener()
                {
                    @Override
                    public void propertyChange( PropertyChangeEvent evt )
                    {
                        if ( "DONE".equals( evt.getNewValue().toString() ) )
                        {
                        try {
                            fetchEmpListDone(task.get());
                        } catch (InterruptedException ex) {
                            Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ExecutionException ex) {
                            Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                    }
                }
            );
            task.execute();
        }

        private void fetchStatusList()
        {
            final fetchStatusList task = new fetchStatusList();
            task.addPropertyChangeListener
            (
                new PropertyChangeListener()
                {
                    @Override
                    public void propertyChange( PropertyChangeEvent evt )
                    {
                        if ( "DONE".equals( evt.getNewValue().toString() ) )
                        {
                        try{
                            fetchStatusListDone( task.get() );
                        } catch (InterruptedException ex)
                        {
                            Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ExecutionException ex)
                        {
                            Logger.getLogger(mainView.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                    }
                }
            );
            task.execute();
        }

        private void fetchEmpListDone( List<Emp> empList )
        {
            for( Emp e : empList )
            {
                //removes non-current employees
                if ( e.getEmpQuit() == null )
                {
                    this.empList.add(e);
                }
                //employee 1000: No Owner
                if ( e.getEmpNum() == 1000 )
                {
                    this.noEmp = e;
                }
                //sets default emp
                if ( e.getEmpNum() == this.defaultEmpId )
                {
                    this.defaultEmp = e;
                }
            }
            this.empListFetched = true;
        }

        private void fetchStatusListDone( List<JobStatus> statusList )
        {
            //only add status types that are not in the exception list
            for( JobStatus js : statusList )
            {
                this.statusList.add(js);

                if ( !this.statusExceptionList.contains(js.getId()) )
                {
                    this.adjustedStatusList.add(js);
                }
            }
            this.statusListFetched = true;
        }


        @Override
        public ArrayList<Integer> getStatusExceptionList()
        {
            return this.statusExceptionList;
        }

        @Override
        public ArrayList<JobStatus> getAdjustedStatusList()
        {
            return this.adjustedStatusList;
        }

        @Override
        public ArrayList<Integer> getStatusLockedList()
        {
            return this.statusLockedList;
        }

        @Override
        public String getDefaultPrinter()
        {
            return this.defaultPrinter;
        }

        @Override
        public Emp getDefaultEmp()
        {
            return this.defaultEmp;
        }

        @Override
        public Emp getNoEmp()
        {
            return this.noEmp;
        }

        @Override
        public ArrayList<Emp> getEmpList()
        {
            return this.empList;
        }

        @Override
        public boolean empListFetched()
        {
            return this.empListFetched;
        }

        @Override
        public List<JobStatus> getStatusList()
        {
            return this.statusList;
        }

        @Override
        public boolean statusListFetched()
        {
            return this.statusListFetched;
        }

        @Override
        public EntityManager getEM()
        {
            return this.em;
        }

        @Override
        public JComponent getParentContainer()
        {
            return this.mainPanel;
        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")                        
        private void initComponents() {

            myCookieLabel = new javax.swing.JLabel();
            mainPanel = new javax.swing.JPanel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("myCookie");
            setMinimumSize(new java.awt.Dimension(640, 480));
            setResizable(false);
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

            myCookieLabel.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.disabledShadow"));
            myCookieLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
            myCookieLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            myCookieLabel.setText("Steve Pro");
            getContentPane().add(myCookieLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 640, 40));

            mainPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
            mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
            getContentPane().add(mainPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 40, 640, 440));

            pack();
        }                       

        /**
        * @param args the command line arguments
        */
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        new mainView();
                    }
                }
            );
        }

        // Variables declaration - do not modify                     
        private javax.swing.JPanel mainPanel;
        private javax.swing.JLabel myCookieLabel;
        // End of variables declaration                   

        @Override
        public void addStringEventListener ( StringEventListener stringEventListener )
        {
            listenerList.add( stringEventListener );
        }

        @Override
        public void removeStringEventListener ( StringEventListener stringEventListener )
        {
            listenerList.remove( stringEventListener );
        }

        private void fireStringEvent( String string )
        {
            for ( EventListener l : listenerList )
            {
                if ( l instanceof StringEventListener )
                {
                    ((StringEventListener) l).stringEventFired(string);
                }
            }
        }
    }

尝试将main方法放在一个名为“main”的类中,也许Java 8会查找一个main类。请显示完整的类定义,jar中的
pom.xml
MANIFEST
,MANIFEST的类路径中的第一个条目不应该是“mycokie-1.0.jar”。目前它缺少“.jar”结尾,但我认为maven可能会生成一个以.jar结尾的文件。编辑:但是jar文件本身包含在类路径中是没有意义的,所以忘记这个注释吧。作为背景,在我发布这个问题之前,我打算从MF中完全删除mycokie条目。这是我最后的努力,我试图强迫JVM找到mycokie.jar中的文件,但不幸的是,即使将JVM直接指向jar,对Java8也没有帮助。它不应该在那里,我还尝试将当前目录添加到类路径中,但也没有任何效果。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.glsarchives</groupId>
    <artifactId>myCookie</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.netbeans.external</groupId>
            <artifactId>AbsoluteLayout</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>cecore</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>celib</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>ceplugins</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>cereports</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>cesession</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>ceutils</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.dspace.xmlui.concurrent</groupId>
            <artifactId>Concurrent</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>corbaidl</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.33-bin</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.businessobjects.sdk</groupId>
            <artifactId>rascore</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.9.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>xml-apis</artifactId>
            <version>0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.glsarchives</groupId>
            <artifactId>GLSLib</artifactId>
            <version>1.1.2-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    <!--Build Settings-->
    <build>
        <sourceDirectory>./src</sourceDirectory>
        <finalName>myCookie-${project.version}</finalName>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!-- Maven-Dependency-Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven-Jar-Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>main.java.myCookie.mainView</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/</Class-Path>
                            <Class-Path>myCookie-1.0</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!-- Maven Resources Plugin -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                <!-- Copies resource files that can be accessed by the user (unpackaged resources), such as configuration and properties files to the installation directory -->
                    <execution>
                        <id>copy-internal-resources</id>
                        <phase>validate</phase>
                        <goals>
                                <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                                <outputDirectory>${basedir}/target</outputDirectory>
                                <resources>          
                                        <resource>
                                                <includes>
                                                        <include>**/*.properties</include>
                                                </includes>
                                                <directory>${basedir}/src/main/resources/external</directory>
                                                <targetPath>${project.build.directory}/</targetPath>
                                        </resource>
                                </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/java/gls_desktop/resources</directory>
                <targetPath>main/java/gls_desktop/resources</targetPath>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/java/gls_desktop/xml</directory>
                <targetPath>main/java/gls_desktop/xml</targetPath>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/java/gls_desktop/jCalendar</directory>
                <targetPath>main/java/gls_desktop/jCalendar</targetPath>
                <includes>
                    <include>Bundle.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/java/gls_desktop/jCalendar/images</directory>
                <targetPath>main/java/gls_desktop/jCalendar/images</targetPath>
            </resource>
        </resources>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: bsnider
Class-Path: myCookie-1.0 lib/AbsoluteLayout-0.0.jar lib/cecore-0.0.jar
  lib/celib-0.0.jar lib/ceplugins-0.0.jar lib/cereports-0.0.jar lib/ce
 session-0.0.jar lib/ceutils-0.0.jar lib/commons-logging-1.1.1.jar lib
 /Concurrent-0.0.jar lib/corbaidl-0.0.jar lib/eclipselink-2.5.2.jar li
 b/javax.persistence-0.0.jar lib/log4j-1.2.17.jar lib/mysql-connector-
 java-5.1.33-bin.jar lib/rascore-0.0.jar lib/xercesImpl-2.9.1.jar lib/
 xml-apis-1.3.04.jar lib/xml-apis-0.0.jar lib/GLSLib-1.1.2-20170210.20
 0207-1.jar lib/commons-lang-2.1.jar lib/serialization.jar-0.0.jar lib
 /ebus405-0.0.jar lib/MetafileRenderer-0.0.jar lib/rasapp-0.0.jar lib/
 javax.mail-1.5.0.jar lib/activation-1.1.jar lib/org.eclipse.persisten
 ce.jpa-2.6.4.jar lib/org.eclipse.persistence.asm-2.6.4.jar lib/org.ec
 lipse.persistence.antlr-2.6.4.jar lib/javax.json-1.0.4.jar lib/org.ec
 lipse.persistence.jpa.jpql-2.6.4.jar lib/org.eclipse.persistence.core
 -2.6.4.jar lib/imgscalr-lib-4.2.jar lib/awtextra-0.0.jar lib/ReportPr
 inter-0.0.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_101
Main-Class: main.java.myCookie.mainView