我有一个 python 脚本,它从我的持续集成服务器中获取一个 Java jar 文件,并在每次构建后启动它。但是,当我尝试运行 Jar 文件时出现一个奇怪的错误,“[错误 2] 系统找不到指定的文件”。现在,如果我要使用的文件丢失或路径错误,这将是有意义的,但 os.path.exists() 在 WindowsError 发生之前在文件上返回 true。所以,我想知道找不到哪个文件......也许我的启动命令的某些部分被解释为文件参数(可以理解的是它会丢失)?所以我的问题是,在错误 2 的情况下,我如何发现 Windows 找不到哪个文件?
为确保我的问题与脚本中的其他错误无关,以下是相关位:
服务器常量.py:
import os
__author__ = 'Brendon Dugan'
serverJarName = "eloquence-server.jar"
backupJarName = "eloquence-server-backup.jar"
deployedJarName = "eloquence-server-jar-with-dependencies.jar"
serverLocation = os.path.join("C:" + os.path.sep, "eloquence-alpha")
serverFlag = "-isServer=true"
javaVariant = "javaw"
javaExecutable = os.path.join(os.environ["JAVA_HOME"], "bin", javaVariant)
serverStartCommand = ("start \"Eloquence Server\" /d" +
serverLocation + " \"" + javaExecutable + "\" -jar " +
os.path.join(serverLocation, serverJarName) + " " + serverFlag)
启动服务器.py:
import os
import subprocess
import traceback
import psutil
import serverConstants
def startServer(server):
if os.path.exists(server):
print "Server seems to exist"
os.chdir(serverConstants.serverLocation)
kwargs = {}
DETACHED_PROCESS = 0x00000008
CREATE_NEW_PROCESS_GROUP = 0x00000200
kwargs.update(creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
try:
print serverConstants.serverStartCommand
print os.getcwd()
eloquenceProcess = subprocess.Popen(serverConstants.serverStartCommand, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs)
print eloquenceProcess.pid
print "Server started successfully"
return 0
except WindowsError, error:
print "A Windows Error Has Occurred"
print error.strerror
print error
print "An error occurred while starting the server"
return 1
if __name__ == "__main__":
print "Preparing to start server"
server = os.path.join(serverConstants.serverLocation, serverConstants.serverJarName)
print server
if startServer(server) == 0:
exit(0)
exit(1)
完整程序输出:
Preparing to start server
C:\eloquence-alpha\eloquence-server.jar
Server seems to exist
start "Eloquence Server" /dC:\eloquence-alpha "C:\Program Files\Java\jdk1.7.0_11\bin\javaw" -jar C:\eloquence-alpha\eloquence-server.jar -isServer=true
C:\eloquence-alpha
A Windows Error Has Occurred
The system cannot find the file specified
[Error 2] The system cannot find the file specified
An error occurred while starting the server
Process finished with exit code 1