简体   繁体   中英

Java unable to open main class and jar file

I've written a Java chat application using client/server architecture. As a build tool I've used 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

java Main 

from {basedir}/target/classes (Main is my main class), nor with command opening the jar file

java -jar ChatServer-1.0-SNAPSHOT

form {basedir}/target. I haven't created any packages for my classes, they are placed in {basedir}/src/main/java directory. When I try load my Main class, this is the output:

Error: Could not find or load main class Main

When I try to open my jar file, this is the output:

Unable to access jarfile ChatServer-1.0-SNAPSHOT

The whole project runs really well when I run it in IntelliJ. What am I doing wrong?

Edit:
I've moved my classes to pl.dsdev.server package, when I run

java -jar ChatServer-1.0-SNAPSHOT.jar 

this is the output:

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

My 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. I don't know what was the reason, but after I did

mvn clean package 

and tried to run it again it finally worked.

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