I need to execute a bat file that executes a PostgreSQL query in commandline using psql.exe in windows 10 and Java-8 .I have to execute the query through bat file only for testing the bat file. Below is the contents of the bat file
BatFileContents:
postgresql\bin\psql.exe -U username -p dbport -h 127.0.0.1 -c "
insert into table1 values(value1,value2);
insert into table2 values(value1,value2);
insert into table3 values(value1,value2) database"
I tried using java ProcessBuilder to call the bat file but pgsql is asking for password.Below is the code i used to call the bat file and provide password but it doesn't work as expected.
Code:
Main class:
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class ProcessTest {
public static void main(String args[])
{
Process process=null;
String batFile="\"c:\\batfilepath\\test.bat\"";
ProcessBuilder processBuilder=new ProcessBuilder(batFile);
try {
//process=Runtime.getRuntime().exec(batFile);
process=processBuilder.start();
StreamEater inputStreamEater=new StreamEater(process.getInputStream());
StreamEater errorStreamEater=new StreamEater(process.getErrorStream());
inputStreamEater.start();
errorStreamEater.start();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
writer.write("password");
writer.newLine();
writer.close();
int errorcode=process.waitFor();
System.out.println("errorcode:"+errorcode);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
StreamEater class
class StreamEater extends Thread
{
private InputStream inputStream=null;
public StreamEater(InputStream stream)
{
this.inputStream=stream;
}
public void run()
{
System.out.println("Stream Eater thread started");
Scanner scanner=new Scanner(new InputStreamReader(inputStream));
String message="";
StringBuilder sb=new StringBuilder("");
try
{
System.out.println("before reading message");
while(scanner.hasNextLine())
{
message=scanner.nextLine();
sb.append(message);
sb.append("\n");
}
scanner.close();
System.out.println("after reading message "+sb.toString());
}
catch(Exception exception)
{
exception.printStackTrace();
}
System.out.println("Stream Eater thread ended");
}
}
Can anyone point out what mistake i have done.Is there any other way to send input to the bat file.
There are several issues with this code. I will answer your direct questions first.
Passing Password Arguments (Direct Answer)
The pgsql command forces the user to enter a password interactively. This SO answer describes two techniques to handle this:
Use a .pgpass file. You can create this file independently of your program and alter the BAT file to reference it.
Set a PGPASSWORD environment variable. You can alter the BAT file to do this. You can also set this in ProcessBuilder from your application with code like processBuilder.environment().put("PGPASSWORD ", "PASSWORD");
Use JDBC (Improve your code)
Instead of having your Java app call a batch file, use JDBC. JDBC literally exists so that Java can connect to a database and run queries against that database.
This would avoid the challenges of calling a batch script and let your application directly interact with the database.
I think that your thread will end really soon since the scanner has no lines to read. It could be blocked operation though but you should loop this thread. The answer above is also good but try this without affecting you code too much:
while (stillReading) {
while(scanner.hasNextLine())
{
message=scanner.nextLine();
sb.append(message);
sb.append("\n");
}
Thread.sleep(1000);
}
The main problem is that there is no input in InputStream when the thread starts.
I made spark+hadoop yarn enviroment and spark-submit command works well. So I made SparkLauncher java code to do this in my application jar, BUT somehow it doesn't work (actually computer fan is spinning at first but not as long as i did with spark-submit.)
It seems not work well (no application log in hadoop web ui, unlike spark-submit). I cannot see any error log when I do with 'SparkLauncher'. without log message, I can do nothing with it.
Here is how I made it so far.
public class Main {
public static void main(String[] args) {
Process spark = null;
try
{
spark = new SparkLauncher()
.setAppResource("/usr/local/spark/examples/jars/spark-examples*.jar")
.setMainClass("org.apache.spark.examples.SparkPi")
.setMaster("yarn")
.setDeployMode( "cluster")
.launch();
}
catch( IOException e)
{
e.printStackTrace();
}
}
}
executed it with ( java -jar example.jar)
I had the same problem at first. I think the main issue is that you forgot about the waitFor().
Also, it's really helpfull to extract your errorMessage and deal with it (e.g. log it or checking it while debuging ) within your java code. To allow this, you should create a streamReader thread as follows:
InputStreamReaderRunnable errorStreamReaderRunnable = new InputStreamReaderRunnable(spark.getErrorStream(), "error");
Thread errorThread = new Thread(errorStreamReaderRunnable, "LogStreamReader error");
errorThread.start();
int result= spark.waitFor();
if(result!=0) {
String errorMessage = extractExceptionMessage(errorStreamReaderRunnable.getMessage());
LOGGER.error(errorMessage);
}
This should be after your launch() command and inside your try block. Hope it helps
I am trying to run AWS-CLI from a batch script to sync files with S3, then automatically close the cmd window.
In all my batch scripts without AWS-CLI involved the Process.waitFor method will cause the cmd window to automatically exit upon process execution completion, but this is not the case when I have an AWS CLI command in there.
The S3 Sync will finish and I will be left with an open cmd window, and the program will not continue until I manually close it.
Is there something special I need to do in order to make Process.waitFor work in this case, or otherwise automatically close the cmd window upon script completion?
This question is unique because the command normally returns just fine, but is not in the specific case of using AWS CLI.
You're probably not reading the process output, so it's blocked trying to write to stdout.
This works for me:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;
public class S3SyncProcess {
public static void main(String[] args) throws IOException, InterruptedException {
// sync dir
Process process = Runtime.getRuntime().exec(
new String[] {"aws", "s3", "sync", "dir", "s3://my.bucket"}
);
CompletableFuture.runAsync(() -> pipe(process.getInputStream(), System.out));
CompletableFuture.runAsync(() -> pipe(process.getErrorStream(), System.err));
// Wait for exit
System.exit(process.waitFor());
}
private static void pipe(InputStream in, OutputStream out) {
int c;
try {
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
// ignore
}
}
}
I can't for the life of me figure out how to start a nd stop a server for the game Minecraft using two buttons in Java.
So far I have this mess..
try
{
ProcessBuilder processBuilder = new ProcessBuilder("/Users/UserName/Desktop/servers/test/launch.sh");
Process server;
if (event.getSource() == start_Btn)
{
server = processBuilder.start();
//OutputStream out = server.getOutputStream();
start_Btn.setText("Started");
}
else if (event.getSource() == stop_Btn)
{
OutputStream out = server.getOutputStream();
server.getOutputStream().write(new String("stop").getBytes("utf-8"));
stop_Btn.setText("Stoped");
start_Btn.setText("Start");
}
}
catch (IOException exception)
{
}
catch (InterruptedException exception)
{
}
I have been scouring the internet for the entire day today and I've decided to finally bring it to you guys.
I want to be able to start the server by pressing a "Start" button, then stop it with a "Stop" button I have a GUI set up and I know how to set up button events. I can get the server to start with the start button easily, it is just the stopping feature I can't seem to manage.
Note: To stop the server you must enter in "stop" in the command line where the server was initiated.
Thank you very much for your help, I greatly appreciate it.
Seeing as though there never was an answer that solved my question, and seeing as though I figured it out on my own I figured I'd post it for everyone else happening on the question.
I use a couple classes to accomplish this goal, two to be exact.. One to be the thread that houses the server and the other to send commands to the server.
First things first, the code to start and house the server stream.
The first class here is where the magic happens
public class Sender{
ConsoleWriter cWriter = new ConsoleWriter();
public void execute(){
this.ui = ui;
try{
ProcessBuilder pb = new ProcessBuilder(path_to_server+"launch.bat");
Process process = pb.start();
StreamGobbler sgError = new StreamGobbler(process.getErrorStream());
new Thread( sgError ).start();
writer = new PrintWriter( process.getOutputStream() );
} catch ( IOException e ){
e.printStackTrace();
}
}
private class StreamGobbler implements Runnable
{
private InputStream is;
public StreamGobbler( InputStream is ){
this.is = is;
}
#Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String line = null;
while ( ( line = br.readLine() ) != null ){
cWriter.writer(line, ui);
}
} catch ( IOException e ){
e.printStackTrace();
}
}
}
}
How does this work you ask? Let's take it from the top.
ConsoleWriter
This is the class I wrote to write the stream from Minecraft's server console to my own GUI, the code for this isn't important so I'll leave it out. You can write that part.
execute()
This method builds the process for the server using Java's ProcessBuilder then starting the process. More importantly, we have the StreamGobbler.. This gives us access to and 'gobbles up' the input stream from the server. Basically it receives all the output the console from the server. For some reason, not sure why, the Minecraft server likes the ErrorStream so I've bound it to that. Then I create a new Thread for the gobbler and that's that. Last thing in this method is the...
PrinterWriter
This binds to the Server as an output which let's me send commands to the server like for stopping it or really any other server command available.
StreamGobbler class
Now, onto the Gobbler its self. Not too much here. Basically just taking the inputStream we sent from the execute method sending it to a reader, then buffering it and finally reading it to my console writer.
The second Class is quite simple!
public class WriteCommand
{
public void send(String command)
{
txtA.append("Command:>>"+ command + "\n");
writer.write(command);
writer.write("\n");
writer.flush();
}
}
All this is doing is writing the command and hitting 'enter' then flushing it to be ready to send the next! txtA.append is for adding the command that was sent to my own console output simply a visual item.
And there you go! Hopefully this will help someone else out.
If you'd like to see this code in action you can see it as part of the app I've used it in.
Here is a link: Minecraft Server Utility(BETA)1.3.6
I was working on this same task today. I am a novice at java but I think I found what you may be missing.
I more or less followed your lead but in the stop command use the slash "/stop"
also it seems that I needed to close the outputstream in order for the action to complete.
private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
oS.write(new String("/stop").getBytes("utf-8"));
oS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I hope that this helps you.
I want to prevent the user from running my java application multiple times in parallel.
To prevent this, I have created a lock file when am opening the application, and delete the lock file when closing the application.
When the application is running, you can not open an another instance of jar. However, if you kill the application through task manager, the window closing event in the application is not triggered and the lock file is not deleted.
How can I make sure the lock file method works or what other mechanism could I use?
You could use a FileLock, this also works in environments where multiple users share ports:
String userHome = System.getProperty("user.home");
File file = new File(userHome, "my.lock");
try {
FileChannel fc = FileChannel.open(file.toPath(),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
FileLock lock = fc.tryLock();
if (lock == null) {
System.out.println("another instance is running");
}
} catch (IOException e) {
throw new Error(e);
}
Also survives Garbage Collection.
The lock is released once your process ends, doesn't matter if regular exit or crash or whatever.
Similar discussion is at
http://www.daniweb.com/software-development/java/threads/83331
Bind a ServerSocket. If it fails to bind then abort the startup. Since a ServerSocket can be bound only once, only single instsances of the program will be able to run.
And before you ask, no. Just because you bind a ServerSocket, does not mean you are open to network traffic. That only comes into effect once the program starts "listening" to the port with accept().
I see two options you can try:
Use a Java shutdown hook
Have your lock file hold the main process number. The process should exist when you lanuch another instance. If it's not found in your system, you can assume that the lock can be dismissed and overwritten.
Creating a server socket, bounds to a specific port with a ServerSocket instance as the application starts is a straight way.
Note that ServerSocket.accept() blocks, so running it in its own thread makes sense to not block the main Thread.
Here is an example with a exception thrown as detected :
public static void main(String[] args) {
assertNoOtherInstanceRunning();
... // application code then
}
public static void assertNoOtherInstanceRunning() {
new Thread(() -> {
try {
new ServerSocket(9000).accept();
} catch (IOException e) {
throw new RuntimeException("the application is probably already started", e);
}
}).start();
}
You could write the process id of the process that created the lock file into the file.
When you encounter an existing lock file, you do not just quit, but you check if the process with that id is still alive. If not, you can go ahead.
You can create a Server socket like
new ServerSocket(65535, 1, InetAddress.getLocalHost());
at very beginning of your code. Then if AddressAlreadyInUse exception caught in main block you can display the appropriate message.
There are already available java methods in File class to achieve the same. The method is deleteOnExit() which ensure the file is automatically deleted when the JVM exits. However, it does not cater to forcible terminations. One should use FileLock in case of forcible termination.
For more details check, https://docs.oracle.com/javase/7/docs/api/java/io/File.html
Thus code snippet which could be used in the main method can be like :
public static void main(String args[]) throws Exception {
File f = new File("checkFile");
if (!f.exists()) {
f.createNewFile();
} else {
System.out.println("App already running" );
return;
}
f.deleteOnExit();
// whatever your app is supposed to do
System.out.println("Blah Blah")
}
..what other mechanism could I use?
If the app. has a GUI it can be launched using Java Web Start. The JNLP API provided to web-start offers the SingleInstanceService. Here is my demo. of the SingleInstanceService.
You can write something like this.
If file exists try to delete it. if it is not able to delete. We can say that application is already running.
Now create the same file again and redirect the sysout and syserr.
This works for me
Simple lock and advanced lock
I developed 2 solutions for this problem. I was also looking for an easy way of doing this without using any libraries and a lot of code.
My solutions are based on: https://stackoverflow.com/a/46705579/10686802 which I have improved upon. Therefore I would like to thank #akshaya pandey and #rbento
Simple file lock
package YOUR_PACKAGE_NAME;
import java.io.File;
import java.io.IOException;
/**
* Minimal reproducible example (MRE) - Example of a simple lock file.
* #author Remzi Cavdar - ict#remzi.info - #Remzi1993
*/
public class Main {
public static void main(String[] args) {
/*
* Prevents the user of starting multiple instances of the application.
* This is done by creating a temporary file in the app directory.
* The temp file should be excluded from git and is called App.lock in this example.
*/
final File FILE = new File("App.lock");
try {
if (FILE.createNewFile()) {
System.out.println("Starting application");
} else {
System.err.println("The application is already running!");
return;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
/*
* Register a shutdown hook to delete the lock file when the application is closed. Even when forcefully closed
* with the task manager. (Tested on Windows 11 with JavaFX 19)
*/
FILE.deleteOnExit();
// Whatever your app is supposed to do
}
}
Advanced lock
package YOUR_PACKAGE_NAME;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
/**
* Minimal reproducible example (MRE) - Example of a more advanced lock system.
* #author Remzi Cavdar - ict#remzi.info - #Remzi1993
*/
public class Main {
public static void main(String[] args) {
/*
* Prevents the user of starting multiple instances of the application.
* This is done by creating a temporary file in the app directory.
* The temp file should be excluded from git and is called App.lock in this example.
*/
final File FILE = new File("App.lock");
if (FILE.exists()) {
System.err.println("The application is already running!");
return;
}
try (
FileOutputStream fileOutputStream = new FileOutputStream(FILE);
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock()
) {
System.out.println("Starting application");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
/*
* Register a shutdown hook to delete the lock file when the application is closed. Even when forcefully closed
* with the task manager. (Tested on Windows 11 with JavaFX 19)
*/
FILE.deleteOnExit();
// Whatever your app is supposed to do
}
}
Testing
Tested on: 31-10-2022
Tested OS: Windows 11 - Version 21H2 (OS Build 22000.1098)
Tested with: OpenJDK 19 - Eclipse Temurin JDK with Hotspot 19+36(x64)
I closed the application and also forcefully closed the application with task manager on Windows both times the lock file seems to be deleted upon (force) close.
I struggled with this same problem for a while... none of the ideas presented here worked for me. In all cases, the lock (file, socket or otherwise) did not persist into the 2nd process instance, so the 2nd instance still ran.
So I decided to try an old school approach to simply crate a .pid file with the process id of the first process. Then any 2nd process would quit if it finds the .pid file, and also the process number specified in the file is confirmed to be still running. This approach worked for me.
There is a fair bit of code, which I provide here in full for your use... a complete solution.
package common.environment;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.*;
import java.nio.charset.Charset;
public class SingleAppInstance
{
private static final #Nonnull Logger log = LogManager.getLogger(SingleAppInstance.class.getName());
/**
* Enforces that only a single instance of the given component is running. This
* is resilient to crashes, unexpected reboots and other forceful termination
* scenarios.
*
* #param componentName = Name of this component, for disambiguation with other
* components that may run simultaneously with this one.
* #return = true if the program is the only instance and is allowed to run.
*/
public static boolean isOnlyInstanceOf(#Nonnull String componentName)
{
boolean result = false;
// Make sure the directory exists
String dirPath = getHomePath();
try
{
FileUtil.createDirectories(dirPath);
}
catch (IOException e)
{
throw new RuntimeException(String.format("Unable to create directory: [%s]", dirPath));
}
File pidFile = new File(dirPath, componentName + ".pid");
// Try to read a prior, existing pid from the pid file. Returns null if the file doesn't exist.
String oldPid = FileUtil.readFile(pidFile);
// See if such a process is running.
if (oldPid != null && ProcessChecker.isStillAllive(oldPid))
{
log.error(String.format("An instance of %s is already running", componentName));
}
// If that process isn't running, create a new lock file for the current process.
else
{
// Write current pid to the file.
long thisPid = ProcessHandle.current().pid();
FileUtil.createFile(pidFile.getAbsolutePath(), String.valueOf(thisPid));
// Try to be tidy. Note: This won't happen on exit if forcibly terminated, so we don't depend on it.
pidFile.deleteOnExit();
result = true;
}
return result;
}
public static #Nonnull String getHomePath()
{
// Returns a path like C:/Users/Person/
return System.getProperty("user.home") + "/";
}
}
class ProcessChecker
{
private static final #Nonnull Logger log = LogManager.getLogger(io.cpucoin.core.platform.ProcessChecker.class.getName());
static boolean isStillAllive(#Nonnull String pidStr)
{
String OS = System.getProperty("os.name").toLowerCase();
String command;
if (OS.contains("win"))
{
log.debug("Check alive Windows mode. Pid: [{}]", pidStr);
command = "cmd /c tasklist /FI \"PID eq " + pidStr + "\"";
}
else if (OS.contains("nix") || OS.contains("nux"))
{
log.debug("Check alive Linux/Unix mode. Pid: [{}]", pidStr);
command = "ps -p " + pidStr;
}
else
{
log.warn("Unsupported OS: Check alive for Pid: [{}] return false", pidStr);
return false;
}
return isProcessIdRunning(pidStr, command); // call generic implementation
}
private static boolean isProcessIdRunning(#Nonnull String pid, #Nonnull String command)
{
log.debug("Command [{}]", command);
try
{
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
InputStreamReader isReader = new InputStreamReader(pr.getInputStream());
BufferedReader bReader = new BufferedReader(isReader);
String strLine;
while ((strLine = bReader.readLine()) != null)
{
if (strLine.contains(" " + pid + " "))
{
return true;
}
}
return false;
}
catch (Exception ex)
{
log.warn("Got exception using system command [{}].", command, ex);
return true;
}
}
}
class FileUtil
{
static void createDirectories(#Nonnull String dirPath) throws IOException
{
File dir = new File(dirPath);
if (dir.mkdirs()) /* If false, directories already exist so nothing to do. */
{
if (!dir.exists())
{
throw new IOException(String.format("Failed to create directory (access permissions problem?): [%s]", dirPath));
}
}
}
static void createFile(#Nonnull String fullPathToFile, #Nonnull String contents)
{
try (PrintWriter writer = new PrintWriter(fullPathToFile, Charset.defaultCharset()))
{
writer.print(contents);
}
catch (IOException e)
{
throw new RuntimeException(String.format("Unable to create file at %s! %s", fullPathToFile, e.getMessage()), e);
}
}
static #Nullable String readFile(#Nonnull File file)
{
try
{
try (BufferedReader fileReader = new BufferedReader(new FileReader(file)))
{
StringBuilder result = new StringBuilder();
String line;
while ((line = fileReader.readLine()) != null)
{
result.append(line);
if (fileReader.ready())
result.append("\n");
}
return result.toString();
}
}
catch (IOException e)
{
return null;
}
}
}
To use it, simply invoke it like this:
if (!SingleAppInstance.isOnlyInstanceOf("my-component"))
{
// quit
}
I hope you find this helpful.
Finally I found really simple library to achieve this. You can you use JUniqe.
The JUnique library can be used to prevent a user to run at the same
time more instances of the same Java application.
This is an example how to use it from the documentation
public static void main(String[] args) {
String appId = "myapplicationid";
boolean alreadyRunning;
try {
JUnique.acquireLock(appId);
alreadyRunning = false;
} catch (AlreadyLockedException e) {
alreadyRunning = true;
}
if (!alreadyRunning) {
// Start sequence here
}
}
here is a pretty rudimental approach.
If your application is launched from a script, check the running java/javaw processes and their command line before launch
In windows
REM check if there is a javaw process running your.main.class
REM if found, go to the end of the script and skip the launch of a new instance
WMIC path win32_process WHERE "Name='javaw.exe'" get CommandLine 2>nul | findstr your.main.class >nul 2>&1
if %ERRORLEVEL% EQU 0 goto:eof
javaw your.main.class