简体   繁体   English

在编译我的第一个Java应用程序时出现问题

[英]Problems compiling my first java Application

I'm newbie with Java and I don't know if this is because I'm doing something wrong. 我是Java的新手,我不知道这是否是因为我做错了什么。

My app is not too complicated. 我的应用程序不太复杂。 It has 5 packages. 它有5个程序包。 One of them, the carlosgoce package has the App class with the main method. 其中之一, carlosgoce软件包具有带main方法的App类。 From there, I instantiate my Principal class which extends JFrame on package gui.Principal . 从那里,我实例化了Principal类,该类在gui.Principal包上扩展了JFrame

So, my App class is something like this: 因此,我的App类是这样的:

package carlosgoce;

import javax.swing.JFrame;
import gui.Principal;

public class App {
    public static void main(String[] args) {    
        Principal ventana = new Principal();
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        ventana.setLocationRelativeTo(null);

        ventana.setVisible(true);                        
    }        
}

Ventana means Window by the way. Ventana的意思是Window。

So Principal is just a JFrame . 因此Principal只是一个JFrame

My App works perfectly when i click the "play" button on Netbeans. 当我单击Netbeans上的“播放”按钮时,我的应用程序运行完美。 When I try to use the generated jar it just say that it can't be opened. 当我尝试使用生成的jar时,只是说无法打开它。 I check on my project properties and check if the main class was selected, and it was / it is. 我检查我的项目属性,并检查是否选择了main类。 I read on stackoverflow to disable "Compile on save". 我在stackoverflow上阅读以禁用“保存时编译”。 I did, but it didn't help. 我做到了,但没有帮助。

When I try to open the .jar file it just doesn't work. 当我尝试打开.jar文件时,它不起作用。 I tried with Clean/Build also. 我也尝试过清理/构建。

Then I realized that the JFrame class autogenerated by Netbeans also had a main method so I commented it (I don't know if a project can have multiples main methods). 然后我意识到由Netbeans自动生成的JFrame类也有一个main方法,因此我对此进行了评论(我不知道一个项目是否可以有多个main方法)。 The app is still working when launched from Netbeans but not from the jar file. 从Netbeans启动但从jar文件启动时,该应用程序仍在运行。

Maybe it's because of Maven? 也许是因为Maven? First time using it also. 第一次也使用它。 I found that is similar to composer on PHP. 我发现这类似于PHP上的作曲家。 I just add OpenCSV dependency. 我只是添加OpenCSV依赖项。

I just don't know what to do next. 我只是不知道下一步该怎么做。 Some help? 一些帮助?

Thank you! 谢谢!

More info: This is my current pom.xml file: 更多信息:这是我当前的pom.xml文件:

<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>carlosgoce</groupId>
    <artifactId>distribuir.articulos</artifactId>
    <version>1.02-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>distribuir.articulos</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                      <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>carlosgoce.principal.App</mainClass>
                      </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.0</version>
        </dependency>           
    </dependencies>
</project>

If i compile it with clean/build from Netbeans i got this manifest file: 如果我使用Netbeans的clean / build进行编译,则会得到以下清单文件:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: acliente7
Build-Jdk: 1.7.0_25
Main-Class: carlosgoce.principal.App
Class-Path: opencsv-2.0.jar

Looks great but i can't find the opencsv-2.0.jar inside the .jar file and the app it doesn't work. 看起来不错,但我找不到.jar文件中的opencsv-2.0.jar以及无法正常使用的应用程序。

If i compile my project with cleand and then "build with dependencies" i got this manifest file: 如果我用cleaned编译我的项目,然后“使用依赖项进行构建”,我将得到以下清单文件:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: 23.25-b01 (Oracle Corporation)

So the "Class-Path: opencsv-2.0.jar" and more lines with the info of my project are missing. 因此,缺少“类路径:opencsv-2.0.jar”以及与我的项目信息有关的更多行。 BUT the opencsv IS inside my .jar file. 但是opencsv在我的.jar文件中。

Any tip? 有小费吗?

FIXED compiling the app with the command mvn clean compile assembly:single 固定使用命令mvn clean compile assembly编译应用程序:单个

I don't know if i have to do something with the question 我不知道我是否必须对这个问题做些什么

assembly:single is not needed, just add executions configuration to your pom assembly:single不需要assembly:single ,只需将执行配置添加到pom

 <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> 
        <phase>package</phase> 
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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