从Apache Tomcat上部署的Java代码执行命令

从Apache Tomcat上部署的Java代码执行命令,java,windows,apache,batch-file,tomcat,Java,Windows,Apache,Batch File,Tomcat,我试图将DOCX文件转换为PDF文件,发现这个vb脚本代码完美地将DOCX转换为PDF文件,它使用.bat文件生成文件。代码可以通过java执行 我面临一个奇怪的问题,当我在本地机器上执行代码时,会生成文件,但当我在服务器上部署应用程序时,代码执行时没有错误,但不会生成文件 通过java执行命令需要任何权限吗 详情如下: 服务器操作系统:Windows Server 2012 R2标准 应用服务器:ApacheTomcat 7.0.75 代码: 1) 爪哇 以及.bat文件代码: @Echo o

我试图将DOCX文件转换为PDF文件,发现这个vb脚本代码完美地将DOCX转换为PDF文件,它使用.bat文件生成文件。代码可以通过java执行

我面临一个奇怪的问题,当我在本地机器上执行代码时,会生成文件,但当我在服务器上部署应用程序时,代码执行时没有错误,但不会生成文件

通过java执行命令需要任何权限吗

详情如下:

服务器操作系统:Windows Server 2012 R2标准

应用服务器:ApacheTomcat 7.0.75

代码: 1) 爪哇

以及.bat文件代码:

@Echo off
pushd %~dp0
cscript C:\Docx_To_Pdf_Converter\doc2pdf.vbs %1 %2
vbscript代码,实际将docx转换为pdf

Const wdFormatPDF = 17  ' PDF format. 
Const wdFormatXPS = 18  ' XPS format. 
Const WdDoNotSaveChanges = 0

Dim arguments
Set arguments = WScript.Arguments


Function CheckUserArguments()
If arguments.Unnamed.Count < 1 Or arguments.Unnamed.Count > 2 Then
WScript.Echo "Use:"
WScript.Echo "<script> input.doc"
WScript.Echo "<script> input.doc output.pdf"
WScript.Quit 1
End If
End Function

// Transforms a doc to a pdf
Function DocToPdf( docInputFile, pdfOutputFile )

Dim fileSystemObject
Dim wordApplication
Dim wordDocument
Dim wordDocuments
Dim baseFolder

Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Set wordApplication = CreateObject("Word.Application")
Set wordDocuments = wordApplication.Documents
docInputFile = fileSystemObject.GetAbsolutePathName(docInputFile)
baseFolder = fileSystemObject.GetParentFolderName(docInputFile)
If Len(pdfOutputFile) = 0 Then
pdfOutputFile = fileSystemObject.GetBaseName(docInputFile) + ".pdf"

End If

If Len(fileSystemObject.GetParentFolderName(pdfOutputFile)) = 0 Then
pdfOutputFile = baseFolder + "\" + pdfOutputFile

End If
//' Disable any potential macros of the word document.
wordApplication.WordBasic.DisableAutoMacros
// 'from below line the code does not executes
Set wordDocument = wordDocuments.Open(docInputFile) 
wordDocument.SaveAs pdfOutputFile, wdFormatPDF
wordDocument.Close WdDoNotSaveChanges
wordApplication.Quit WdDoNotSaveChanges

Set wordApplication = Nothing
Set fileSystemObject = Nothing
End Function
// ' Execute script
Call CheckUserArguments()
If arguments.Unnamed.Count = 2 Then
Call DocToPdf( arguments.Unnamed.Item(0), arguments.Unnamed.Item(1) )
Else
Call DocToPdf( arguments.Unnamed.Item(0), "" )
End If

Set arguments = Nothing
Const wdFormatPDF=17'PDF格式。
常量wdFormatXPS=18'XPS格式。
Const WdDoNotSaveChanges=0
模糊的论据
Set arguments=WScript.arguments
函数CheckUserArguments()
如果arguments.Unnamed.Count<1或arguments.Unnamed.Count>2,则
Echo“使用:
WScript.Echo“input.doc”
Echo“input.doc output.pdf”
WScript.Quit 1
如果结束
端函数
//将文档转换为pdf
函数DocToPdf(docInputFile、pdfOutputFile)
Dim文件系统对象
模糊词应用
Dim wordDocument
模糊文字文档
Dim baseFolder
设置fileSystemObject=CreateObject(“Scripting.fileSystemObject”)
Set wordApplication=CreateObject(“Word.Application”)
Set wordDocuments=wordApplication.Documents
docInputFile=fileSystemObject.GetAbsolutePathName(docInputFile)
baseFolder=fileSystemObject.GetParentFolderName(docInputFile)
如果Len(pdfOutputFile)=0,则
pdfOutputFile=fileSystemObject.GetBaseName(docInputFile)+“.pdf”
如果结束
如果Len(fileSystemObject.GetParentFolderName(pdfOutputFile))=0,则
pdfOutputFile=baseFolder+“\”+pdfOutputFile
如果结束
//'禁用word文档的任何潜在宏。
wordApplication.WordBasic.DisableAutoMacros
//'从下一行代码不执行
设置wordDocument=wordDocuments.Open(docInputFile)
wordDocument.SaveAs pdfOutputFile,wdFormatPDF
wordDocument.Close WdDoNotSaveChanges
wordApplication.Quit WdDoNotSaveChanges
Set wordApplication=Nothing
设置fileSystemObject=Nothing
端函数
//'执行脚本
调用CheckUserArguments()
如果arguments.Unnamed.Count=2,则
调用DocToPdf(arguments.Unnamed.Item(0),arguments.Unnamed.Item(1))
其他的
调用DocToPdf(参数。未命名。项(0),“”)
如果结束
设置参数=无

由于我们无法访问您的部署服务器,所以无法给您提供100%的保证答案,但我认为这里发生了一些事情

如果由于某种原因,
.bat
文件丢失或不可执行,那么您的Java代码中会出现一个
IOException
。由于没有得到异常,显然找到并执行了
.bat
文件

但是,
.bat
文件中的任何内容都没有按预期执行。缺少
cscript.exe
,或者缺少
.vbs
文件。以编写代码的方式,您不会意识到这一点。您只会看到
waitfor()
方法返回的非零状态,您不必检查它。所以你不知道到底发生了什么

至少应将
waitfor()
方法调用更改为:

int rc = process.waitFor();
System.out.printf("Process returned %d\n", rc);
这将告诉您尝试执行
.bat
文件时的返回状态。如果它不是零,那么你就有问题了,我99.999%肯定你会发现它不是零。要解决此问题,您需要捕获命令的输出。以下是一个高度简化的示例(如无错误处理;由您决定):

注意以上不是生产级代码,只是如何使用API的示例。仅当外部命令不需要重定向输入时,此功能才起作用;如果它确实需要输入,那么您需要在单独的线程中处理输入和输出,以防止死锁


也应考虑使用<代码> PrimeBuudioD>代码>,因为它更灵活。例如,它允许您将进程的输出重定向到附加到日志文件,您可能应该实现该日志文件。

服务器的操作系统是什么?是否安装了转换器?它在同一个目录中吗?你的问题中遗漏了很多信息,没有这些信息,没有人能帮助你。请访问并阅读以了解如何使用此网站。现在,如果有人向你求助,你希望看到哪些信息来帮助你?另外,您还没有显示足够的代码。。。如果抛出异常会发生什么?你听懂了吗?你好,吉姆,很抱歉简短的描述,我已经更新了我的代码。@JimGarrison我已经根据需要对我的问题进行了详细的编辑,仍然没有更新或建议,是因为投了反对票还是需要更多的信息?请让我知道…谢谢。服务器上是否存在
C:\\Docx\u To\u Pdf\u Converter\\doc2pdf.bat
(是否可以在服务器上执行bat文件,不使用Java)?您可以在bat文件中放置一些debug.logs/echos,以查看它挂起的位置。。。(通常,在处理参数时,您还应该使用ProcessBuilder,而不是Runtime.exec…@Ben Yes C:\\Docx\u To\u Pdf\u Converter\\doc2pdf.bat存在于服务器上。从DOS cmd提示符或Java运行时执行代码。当我在服务器上使用ApacheTomcat部署它时,我遇到了一个问题。我已经用vbScript代码更新了代码,希望能有所帮助。谢谢你建议我检查返回值,它是0“零”,我也更新了vbScript代码,并在代码被跳过的地方添加了注释。但当我使用dos cmd promt或java执行它时,整个代码都会被执行并生成文件,当我使用Ap在服务器上运行应用程序时,不会发生这种情况
int rc = process.waitFor();
System.out.printf("Process returned %d\n", rc);
    Process proc = Runtime.getRuntime().exec(command);
    BufferedReader procOutput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String line;
    while((line = procOutput.readLine()) != null)
    {
        System.out.println(line);
        // Or whatever you need to do to in your environment, such
        // as log the output or examine it to ensure the script did
        // what you want
    }
    int rc = proc.waitFor();
    System.out.printf("Process returned %d\n", rc);