我有一个项目,在两个不同的类中有两个主要方法我想使用cmd中的cp选项运行该项目的jar文件,并给出运行其main的方法。例如java -cp myjar-1.0-SNAPSHOT.jar com.xyz.DataLoader
项目本身就是maven项目,在这里它是pom:
<?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.xyz</groupId>
<artifactId>myjar</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.15.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我尝试命令java -cp myjar-1.0-SNAPSHOT.jar com.xyz.DataLoader
时,我收到错误Could not find or load main class com.xyz.DataLoader
任何人都可以帮忙吗?
更新
主要班级:
package com.xyz;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DataLoader {
public static void main(String[] args) {
/*if(args[0] == null ||args[0].trim().isEmpty()){
return;
}*/
try {
parseFile("C:\\Users\\MOEL3\\Downloads\\abc\\access.log");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseFile(String fileName) throws IOException, ParseException {
BufferedReader br =getBufferedReaderAndOpenFile(fileName);
String strLine;
List<LogElement> logElementList=new ArrayList<>();
extractLinesAndAddToLogElementList(br, logElementList);
//Close the input stream
br.close();
}
private static void extractLinesAndAddToLogElementList(BufferedReader br, List<LogElement> logElementList) throws IOException, ParseException {
String strLine;//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
logElementList.add(convertLineToLogElement(strLine));
}
}
private static BufferedReader getBufferedReaderAndOpenFile(String fileName) throws FileNotFoundException {
FileInputStream fstream = new FileInputStream(fileName);
return new BufferedReader(new InputStreamReader(fstream));
}
private static LogElement convertLineToLogElement(String strLine) throws ParseException {
String[] data = strLine.split("\\|");
LogElement element = new LogElement();
element.setIp(data[1]);
element.setRequest(data[2]);
element.setStatus(data[3]);
element.setUserAgent(data[4]);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = formatter.parse(data[0]);
element.setRequestDate(date);
return element;
}
}