2016-04-28 98 views
0

我一直在C#中開發一個web服務,我必須將Word docx文件轉換爲PDF。搜索了很長時間後,我決定使用java庫docx4j來進行轉換。 .jar文件的工作,當我從一個命令行運行如預期,但是當我從我的C#代碼啓動它通過的System.Diagnostics.Process我得到以下錯誤:從C#啓動.jar文件 - .properties文件未找到

log4j:WARN No appenders could be found for logger (org.docx4j.jaxb.Context). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

...

WARN org.docx4j.utils.ResourceUtils .getResource line 84 - Couldn't get resource: docx4j.properties 
WARN org.docx4j.Docx4jProperties .init line 22 - Couldn't find/read docx4j.properties; docx4j.properties not found via classloader. 

的C#代碼進行調用看起來是這樣的:

 Process javaCall = new Process(); 

     var dir = HttpContext.Current.Server.MapPath("~"); 

     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.Arguments = "-Xmx2048m -jar \"" + dir + "\\App_Data\\DocxToPDF.jar\" \"" + sourcePath + "\""; 
     startInfo.FileName = "\"" + System.Configuration.ConfigurationManager.AppSettings["JdkFilePath"] + "\""; 
     startInfo.UseShellExecute = false; 

     javaCall.StartInfo = startInfo; 
     javaCall.Start(); 

     javaCall.WaitForExit(); 

我檢查的.jar文件,並在目錄中的Windows安全設置,並都被設置爲完全權限給所有用戶。我不明白爲什麼我通過手動命令行調用和System.Diagnostics.Process獲得不同的結果。

我真的很感激一些幫助,並會提供更多的信息,如果你需要它。

+0

開放Java的一個Word文檔轉換成PDF?這不可能是最好的解決方案 – ControlAltDel

+0

@ControlAltDel,我的第一個想法是使用Word.Interop庫,但是因爲我的代碼應該在服務器上運行,這是不可能的。我的選擇是爲Aspose購買許可證,在服務器上安裝LibreOffice並使用他們的解決方案或使用此開放源代碼的Java庫。 –

回答

0

好的,所以答案竟然比我預想的要容易。通過將.properties移動到與.jar文件相同的文件夾,而不是將它們嵌套在程序中,設法找到它們...

我想我應該下次嘗試第一次簡單修復,感謝您的時間。

0

您需要設置java.exe可執行文件的環境變量Path或指定java.exe的完整路徑。 ProcessStartInfo ps = new ProcessStartInfo(@「c:\ Program Files \ java \ jdk1.7.0 \ bin \ java.exe」,@「 - jar C:\ Users \ Owner \ Desktop \ myJarFile.jar」);}} Process.Start(ps);

默認情況下,命令行java的認識路徑,但ProcessStartInfodoes不知道是從Java C#

+0

我在上面的C#代碼的第五行指定了我的java.exe的文件路徑,位於 startInfo.FileName = System.Configuration.ConfigurationManager.AppSettings [「JdkFilePath」] –