简体   繁体   English

从C#启动.jar文件-找不到.properties文件

[英]Starting .jar file from C# - .properties file not found

I've been developing a webservice in C# where I have to convert Word docx files to PDF. 我一直在用C#开发Web服务,我必须将Word docx文件转换为PDF。 After searching for a long time I settled on docx4j, a java library, to make the conversion. 经过很长时间的搜索,我决定使用Java库docx4j进行转换。 The .jar file works as expected when I run it from a command line, but when I start it from my C# code through System.Diagnostics.Process I get the following errors: 从命令行运行.jar文件时,它可以按预期工作,但是当我通过System.Diagnostics.Process从C#代码启动它时,出现以下错误:

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.

The C# code making the call looks like this: 进行调用的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();

I've checked the windows security settings on the .jar file and the directory it's in and both are set to full permissions to all users. 我已经检查了.jar文件及其所在目录的Windows安全设置,并且都设置为对所有用户具有完全权限。 I don't understand why I get different results from making a manual command line call and one from System.Diagnostics.Process. 我不明白为什么通过手动命令行调用和从System.Diagnostics.Process获得不同的结果。

I'd really appreciate some help and will provide more info if you need it. 非常感谢您的帮助,如果需要的话,我们会提供更多信息。

Alright, so that answer turned out to be easier than I expected. 好吧,这样的答案比我预期的要容易。 By moving the .properties to the same folder as the .jar file instead of them being nested inside the program managed to find them... 通过将.properties移至与.jar文件相同的文件夹,而不是将它们嵌套在程序中,可以找到它们。

I guess I should try the easy fixes first next time, thanks for your time. 我想我下次应该先尝试简单修复,谢谢您的宝贵时间。

You need to set environment variable Path of java.exe executable or specify the full path of java.exe. 您需要设置环境变量java.exe可执行文件的路径或指定java.exe的完整路径。

ProcessStartInfo ps = new ProcessStartInfo(@"c:\\Program Files\\java\\jdk1.7.0\\bin\\java.exe",@"-jar C:\\Users\\Owner\\Desktop\\myJarFile.jar"); ProcessStartInfo ps =新的ProcessStartInfo(@“ c:\\ Program Files \\ java \\ jdk1.7.0 \\ bin \\ java.exe”,@“-jar C:\\ Users \\ Owner \\ Desktop \\ myJarFile.jar”); Process.Start(ps); Process.Start(ps);

By default command line knows java path but ProcessStartInfodoes not know where is java 默认情况下,命令行知道Java路径,但ProcessStartInfo不知道Java在哪里

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

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