對不起,一般的標題和長的描述要遵循。預先感謝任何閱讀本文並回答的人。Java應用程序在命令行中無法正常工作在日食中很好
我有一個應用程序需要一個VM參數和一個命令行參數。這是一個maven項目。我使用Java應用程序運行配置在eclipse中運行它。沒問題。它會創建一個PDF文件並將System.out()調用實際打印到STDOUT。記錄調用打印到eclipse控制檯。如預期的那樣。
當我建立的Maven(我使用的是Windows 7中的Git的bash shell):
$ mvn clean compile package
的罐子是建立與內部所有的依賴和persistence.xml文件包括在內。我從命令行運行
$ java -jar target/dne-caprs-reports-0.0.1-SNAPSHOT.jar \
-Dgov.ssa.dne.reportdir=C:\temp\ upcoming_changes
沒有錯誤。萬歲。但是...沒有PDF,也沒有關於STDOUT的消息。 STDOUT上應該有一些消息。我在整個文件系統中搜索PDF,以防輸出到某個意想不到的地方。沒有。
我把jar放到一個Linux機器上,並以相同的方式運行(顯然有不同的reportdir)並得到了相同的結果。沒有PDF。沒有STDOUT。沒有日誌記錄(儘管我不知道我是否正確記錄了日誌)。
所以「不工作」意味着應用程序看起來運行正常,除了在STDOUT上沒有輸出並且沒有創建PDF文件。並沒有任何錯誤。
主要類:RunReport.java: -
package gov.ssa.dne.app;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import gov.ssa.dne.reports.ChangeStatusReport;
import gov.ssa.dne.reports.OpenChangesReport;
import gov.ssa.dne.reports.OpenProblemReport;
import gov.ssa.dne.reports.UpcomingChangesReport;
public class RunReport {
public static void main(String[] reports) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("dne-caprs-reports");
EntityManager em = factory.createEntityManager();
if (reports[0].toString().equals("change_status")) {
ChangeStatusReport report = new ChangeStatusReport(em, "DNE Change Status");
System.out.println("CHANGE STATUS REPORT");
report.generateReport();
}
}
}
ChangeStatusReport.java細節簡潔,刪除:
public class ChangeStatusReport {
private EntityManager em;
private List<Sc6cm3rm1> changeList = new ArrayList<Sc6cm3rm1>();
private Cell cell;
private Table table;
private String title;
private static final Logger LOGGER = Logger.getLogger(ChangeStatusReport.class.getName());
public ChangeStatusReport(EntityManager em, String title) {
this.em = em;
this.title = title;
}
public void generateReport() {
Query q = em.createNativeQuery(NativeQueries.DNE_CHANGE_STATUS, Sc6cm3rm1.class);
changeList = q.getResultList();
createPdf();
}
private void createPdf() {
String reportLocation = System.getProperty("gov.ssa.dne.reportdir");
String report = reportLocation + "DNE_Change_Status_" + DneStyles.reportSdf.format(new Date()) + ".pdf";
try {
PdfWriter writer = new PdfWriter(report);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4.rotate());
// Add title
document.add(pdfTitle);
table = new Table(new float[] { 8, 8, 8, 35, 12, 12, 3, 20 });
document.add(table);
document.close();
LOGGER.log(Level.INFO, "report complete " + report);
} catch (Exception e) {
e.printStackTrace();
}
}
}
的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>gov.ssa.dne.nmst</groupId>
<artifactId>dne-caprs-reports</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dne-caprs-reports</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>gov.ssa.dne.app.RunReport</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/persistence.xml</resource>
<file>src/main/java/META-INF/persistence.xml</file>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<LONG LIST OF DEPENDENCIES REMOVED>
</dependencies>
</project>
什麼正在發生 在這?我已閱讀其他解決STDOUT問題的答案,但這些問題與試圖將jar作爲可執行文件運行而不包含「java -jar」部分的人有關。我沒有那樣做。它與我的Maven構建有關嗎?我如何運行它?我想不出有什麼可以改變的。
它看起來像你在Windows機器上運行,但也許使用bash提示?目錄參數看起來很奇怪,你有沒有試過-Dgov.ssa.dne.reportdir = C:\\ temp \\ coming_changes(轉義反斜槓,在即將到來之前刪除空間) –
沒有stdout是特有的。如果你用一個簡單的'hello world'來代替主體,它應該只是'工作'...並且當你遮蔽它時..減少你的問題集,看看你能否在擴展它之前得到一些基本的工作。你絕對應該看到一些東西。 –
@ lane.maxwell我在窗戶上使用「git bash」外殼。這些都是很好和簡單的建議。謝謝你們倆。很明顯,我一直在尋找這個太久了!我從第二個建議開始。果然,一個簡單的java jar只能打印「Hello world!」到STDOUT其實就是這麼做的。我將繼續從那裏發佈任何我發現的內容。 – april26