gpt4 book ai didi

java - 如何使用java代码重启eclipse的tomcat服务器

转载 作者:行者123 更新时间:2023-11-28 23:10:07 25 4
gpt4 key购买 nike

我正在从事一个项目,该项目正在 ms word 文档中编写 ms 访问数据。当我不止一次运行这个项目时,它会覆盖像这样的图像的 word 文档。

the first run everything is ok

but second run it overwrites on first run

我第一次运行项目一切正常,但第二次它覆盖了第一次。我想如果我重新启动服务器或应用程序,问题就会解决。为此,我编写了这段代码

try {
Socket socket=new Socket("localhost",8080);
if(socket.isConnected())
{
socket.close();
System.out.println("Server closed");
}

Runtime.getRuntime().exec(System.getProperty("catalina.home")+"\\bin\\startup.bat");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();

但它并没有像我想要的那样工作。所以我需要你的帮助。

public static void header_footer(String SalesOrder,String type) 抛出 InvalidFormatException, IOException{

XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();   

if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();

XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFTable asd= header.createTable(1, 2);
asd.setTableAlignment(TableRowAlign.LEFT);
XWPFTableRow row;
XWPFTableCell cell,cell2;
CTHeight ht;
CTTrPr trPr;
CTTblWidth cellWidth;
row= asd.getRow(0);
trPr = row.getCtRow().addNewTrPr();
ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(-1070));
cell=row.getCell(0);

XWPFParagraph parag=document.createParagraph();
parag = cell.getParagraphArray(0);
XWPFRun r1=parag.createRun();

//InputStream pic = new FileInputStream(imgFile2);
/* FileReader fileReader = new FileReader(imgFile2);
BufferedReader bufferedReader = new BufferedReader(fileReader);*/
//InputStream pic = new BufferedInputStream(new FileInputStream(imgFile2));

// BufferedReader reader = new BufferedReader(new InputStreamReader(pic));
// Image jpg = Image.getInstance("/pages/jpg/bg-header.jpg");

/* URL imgLogoUrl = access.getClass().getProtectionDomain().getCodeSource().getLocation();
Image imgLogo= Image.getInstance(imgLogoUrl+"../../images/logo.JPG");
*/
InputStream pic = access_connection.class.getResourceAsStream("page1_header.png");

//InputStream pic = new URL(imgFile2).openStream();

r1.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, imgFile2, Units.toEMU(160), Units.toEMU(55));


cellWidth = cell.getCTTc().addNewTcPr().addNewTcW();
cellWidth.setW(BigInteger.valueOf(3500));
cell2=row.getCell(1);
cellWidth = cell2.getCTTc().addNewTcPr().addNewTcW();
cellWidth.setW(BigInteger.valueOf((short)20000));


parag=cell2.getParagraphArray(0);
parag.setVerticalAlignment(TextAlignment.CENTER);
parag.setAlignment(ParagraphAlignment.CENTER);
cell2.setVerticalAlignment(XWPFVertAlign.CENTER);
XWPFRun r2=parag.createRun();
r2.setFontSize(14);
r2.setFontFamily("Calibri");
r2.setText("TURKISH ENGINE CENTER SHOP VISIT REPORT");
r2.addBreak();
r2.setText("ESN "+SalesOrder.substring(1, 7)+" Istanbul,TURKEY");



// headerFooterPolicy.getFirstPageHeader().clearHeaderFooter();

XWPFParagraph paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);


XWPFRun run = paragraph.createRun();

InputStream a;
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph parag2= footer.createParagraph();
XWPFRun run3=parag2.createRun();
XWPFRun run4=parag2.createRun();
run3.setFontFamily("Arial");
run4.setFontFamily("Arial");
run3.setBold(true);
run4.setItalic(true);
run3.setFontSize(8);
run4.setFontSize(9);
run3.setText("U.S. Export Classification: EAR ECCN 9E991");
run3.addBreak();
run4.setText("Printed by TEC Local EVM System");
XWPFParagraph parag3= footer.createParagraph();
XWPFRun run5=parag3.createRun();
parag3.setAlignment(ParagraphAlignment.RIGHT);
run5.setFontSize(7);
run5.setFontFamily("Arial");
run5.setText("Page ");
run5.getCTR().addNewPgNum();


try {


FileOutputStream input = new FileOutputStream(word_saveas_file+SalesOrder+"_"+type+"_Report.docx");

document.write(input);
input.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

这是我的页眉页脚代码。这是其中之一被覆盖。每次我创建不同的 word 文档时,因为 SalesOrder 参数每次都在变化,但它仍然会被覆盖。

最佳答案

尝试以编程方式启动批处理脚本:

包含这段代码:

try {
Runtime.getRuntime().exec("taskkill /F /IM java.exe");
System.exit(0);
} catch (Throwable e) {
Logger.handle(e);
}

这里有一个链接可以帮助您创建 Windows 服务:http://dotnetlearners.com/windowsservice/creating-first-windows-service

这里是如何通过 C# 查看您的服务代码源

    protected override void OnStart(string[] args)
{
if (!File.Exists(fileLocation))
File.Create(fileLocation).Close();
File.AppendAllText(fileLocation, "Service started : " + DateTime.Now.ToString() + Environment.NewLine);
System.Diagnostics.Process.Start(@"C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\startup.exe");
OnStop();
}

protected override void OnStop()
{
File.AppendAllText(fileLocation, "Service stoped : " + DateTime.Now.ToString() + Environment.NewLine);
}

然后在您通过 java 关闭服务器后调用您的服务

try {
Runtime.getRuntime().exec("taskkill /F /IM java.exe");
Runtime.getRuntime().exec("net start servicename");
System.exit(0);
} catch (Throwable e) {
Logger.handle(e);
}

关于java - 如何使用java代码重启eclipse的tomcat服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com