简体   繁体   English

Java无法打开主类和jar文件

[英]Java unable to open main class and jar file

I've written a Java chat application using client/server architecture. 我已经使用客户端/服务器架构编写了Java聊天应用程序。 As a build tool I've used Maven. 作为构建工具,我使用了Maven。 The server side interface is based on command-line and the problem is that I can't run the application by cmd neither with command 服务器端界面是基于命令行的,问题是我无法使用cmd来运行应用程序

java Main 

from {basedir}/target/classes (Main is my main class), nor with command opening the jar file 来自{basedir} / target / classes(Main是我的主类),也不能通过命令打开jar文件

java -jar ChatServer-1.0-SNAPSHOT

form {basedir}/target. 表格{basedir} / target。 I haven't created any packages for my classes, they are placed in {basedir}/src/main/java directory. 我没有为我的课程创建任何软件包,它们被放置在{basedir} / src / main / java目录中。 When I try load my Main class, this is the output: 当我尝试加载我的Main类时,这是输出:

Error: Could not find or load main class Main

When I try to open my jar file, this is the output: 当我尝试打开jar文件时,输出为:

Unable to access jarfile ChatServer-1.0-SNAPSHOT

The whole project runs really well when I run it in IntelliJ. 当我在IntelliJ中运行它时,整个项目运行得非常好。 What am I doing wrong? 我究竟做错了什么?

Edit: 编辑:
I've moved my classes to pl.dsdev.server package, when I run 运行时,我已将类移至pl.dsdev.server包中

java -jar ChatServer-1.0-SNAPSHOT.jar 

this is the output: 这是输出:

no main manifest attribute, in ChatServer-1.0-SNAPSHOT.jar

My pom.xml: 我的pom.xml:

<?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>pl.dsdev.projects</groupId>
<artifactId>ChatServer</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>pl.dsdev.server.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>

</build>

Okey, it is working now. Okey,现在正在运作。 I don't know what was the reason, but after I did 我不知道是什么原因,但之后

mvn clean package 

and tried to run it again it finally worked. 并尝试再次运行它终于成功了。

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

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