2016-09-23 123 views
0

我有一个需求,我需要通过java代码启动和停止postgreSQL服务。我已经写了下面的代码,但我得到以下错误:通过java代码启动和停止postgreSQL服务

System error 5 has occurred. 

    Access is denied. 

    System error 5 has occurred. 

    Access is denied. 

下面是我的代码:

package frontend.guifx.pginstallation; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardCopyOption; 

import common.core.Logger; 

import frontend.guifx.util.ConstPG; 

public class StartAndStopPostgres { 
    public static String version = "9.5"; 
    public static void main(String[] args){ 
     try { 
      System.out.println("Execution starts"); 
      copyPostgreSqlConfFileAndRestartPg(); 
      System.out.println("Execution finished"); 

     } catch (IOException | InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

     private static void copyPostgreSqlConfFileAndRestartPg() throws IOException, InterruptedException { 
      // TODO Auto-generated method stub 
      Path path = Paths.get("data/PGLogs"); 
      //if directory exists? 
      if (!Files.exists(path)) { 
       try { 
        Files.createDirectories(path); 
       } catch (IOException e) { 
        //fail to create directory 
        e.printStackTrace(); 
       } 
      } 
      Logger.print(StartAndStopPostgres.class, new String[] { "Copying postgresql.conf file ........" }); 
      Path source = Paths.get("data/postgresql.windows.conf"); 
      String copyConfFileTo = getInstallationPath(version); 
      copyConfFileTo = copyConfFileTo.substring(0, copyConfFileTo.lastIndexOf("\\")); 
      Path outputDirectoryPath = Paths.get(copyConfFileTo+File.separator+"data"); 
      Files.copy(source, outputDirectoryPath.resolve(outputDirectoryPath.getFileSystem().getPath("postgresql.conf")), StandardCopyOption.REPLACE_EXISTING); 
      Logger.print(StartAndStopPostgres.class, new String[] { "Tunning datbase starts........" }); 

      Runtime rt = Runtime.getRuntime(); 
      final File file = new File(System.getProperty("java.io.tmpdir") + File.separator + ConstPG.CREATE_RESTART_PG_BAT_FILE); 
      PrintWriter writer = new PrintWriter(file, "UTF-8"); 
      writer.println("net stop postgresql-x64-"+version); 
      writer.println("net start postgresql-x64-"+version); 
      writer.close(); 
      String executeSqlCommand = file.getAbsolutePath(); 
      Process process = rt.exec(executeSqlCommand); 

      /*final List<String> commands = new ArrayList<String>(); 
      commands.add("cmd.exe"); 
      commands.add("/C"); 
      commands.add("net stop postgresql-x64-9.5"); 
      commands.add("net start postgresql-x64-9.5"); 
      ProcessBuilder b = new ProcessBuilder(commands); 
      Process process = b.start();*/ 
      //public static final String PG_RESTART_PG_LOG_FILE = PG_LOGS+"/pgRestartProcess.log"; 
      File createPgRestartProcessFile = new File(ConstPG.PG_RESTART_PG_LOG_FILE); 
      redirectProcessExecutionOutput(process, createPgRestartProcessFile); 
      int exitVal = process.waitFor(); 
      Logger.print(StartAndStopPostgres.class, new String[] { "EXIT VALUE after tunning the PostgreSql database :::::::::::::::::::::" + exitVal + " Logs written to file at: " + createPgRestartProcessFile.getAbsolutePath() }); 

     } 

     public static String getInstallationPath(String version) { 
      //public static final String PROGRAMME_FILES = "C:\\Program Files\\"; 
//   public static final String PROGRAMME_FILES_X86 = "C:\\Program Files (x86)\\"; 
//   public static final String POSTGRESQL = "PostgreSQL"; 
//   public static final String PSQL_PATH = "\\bin\\psql.exe"; 

      //Const values used below are as above 

      String psql = findFile(ConstPG.PROGRAMME_FILES, ConstPG.POSTGRESQL + "\\" + version + ConstPG.PSQL_PATH); 
      if (psql == null) { 
       psql = findFile(ConstPG.PROGRAMME_FILES_X86, ConstPG.POSTGRESQL + "\\" + version + ConstPG.PSQL_PATH); 
      } 
      if(psql != null){ 
       psql = psql.substring(0, psql.lastIndexOf("\\")); 
      } 
      return psql; 
     } 

     public static String findFile(String directoryName, String fileName) { 
      File directory = new File(directoryName); 

      // get all the files from a directory 
      File[] fList = directory.listFiles(); 

      String absolutePath; 

      if (fList != null) { 
       for (File file : fList) { 
        if (file.isFile()) { 
         absolutePath = file.getAbsolutePath(); 
         if (absolutePath.contains(fileName)) 
          return (absolutePath); 
        } else if (file.isDirectory()) { 
         absolutePath = findFile(file.getAbsolutePath(), fileName); 
         if (absolutePath != null) 
          return (absolutePath); 
        } 
       } 
      } 
      return (null); 
     } 


     private static void redirectProcessExecutionOutput(Process process, File processFile) throws IOException { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
      String line = null; 
      FileWriter fw = new FileWriter(processFile.getAbsoluteFile()); 
      BufferedWriter bw = new BufferedWriter(fw); 
      while ((line = reader.readLine()) != null) { 
       Logger.print(StartAndStopPostgres.class, new String[] { line }); 
       bw.write(line); 
       bw.newLine(); 
      } 
      bw.close(); 
     } 
} 

如果我开始我的Eclipse作为一个管理员则能正常工作。另外,如果我在命令提示符(它以管理员身份打开,即右键单击命令提示符图标并单击“以管理员身份运行”)上运行启动和停止命令,则它们将成功执行。但是,如果我在正常的命令提示符下运行这些命令(这​​不是以管理员身份打开),那么我也会遇到同样的错误。

请告知是否有解决方案或解决此问题的方法。

+1

你需要给用户运行该程序的权限启动选项并停止服务。这与Java或Postgres无关。这是一个Windows管理问题。 –

+0

我正在使用两种方法来运行星形和停止命令。第一个是,在上面的代码中,我创建了一个批处理文件并写入了这两个命令并执行了批处理文件。在第二种方法中,我将这些命令添加到一个数组列表中,并将数组List传递给processBuilder。 –

+0

有什么办法通过Java程序给予这种privilages? –

回答

0

,在Java中与运行CMD以管理员身份

窗户更换你的代码“commands.add("cmd.exe");”下面的代码,并尝试

commands.add("runas /profile /user:ADMINUSERNAME \"cmd.exe"); 
+0

与此更改我收到以下异常: java.io.IOException:无法运行程序“runas/profile/user:ADMINUSERNAME”cmd.exe“:CreateProcess error = 2,系统找不到指定的文件 \t at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) \t at frontend.guifx.pginstallation.StartAndStopPostgres.main(StartAndStopPostgres.java:25) 引起:java.io.IOException:CreateProcess error = 2,系统找不到指定的文件 –