I have a jar file for which I created a custom c++ launcher script. I can't figure how to permit only one instance of the program to run. Is there a way to detect already running jvm?
Here's the launcher code-
#include <string>
#include <direct.h>
using namespace std;
int main(int arg_count, char* arg_list[]) {
string exeName(arg_list[0]);
string rootDir = exeName.substr(0, exeName.find_last_of("\\"));
string jarFile = ".\\dist\\PlayIt.jar";
string cmd;
string javaCmdFile = ".\\jre\\bin\\javaw.exe";
_chdir(rootDir.c_str());
if (arg_count == 1) {
cmd = "start " + javaCmdFile + " -jar " + jarFile + " visible";
}
else if (arg_count > 1) {
cmd = "start " + javaCmdFile + " -jar " + jarFile + " invisible";
for (int i = 1; i < arg_count; i++) {
cmd += " \"" + string(arg_list[i]) + "\"";
}
}
system(cmd.c_str());
}
jps command in /jdk/bin lists all running java processes. if you want to make sure no other processes are running you can use pkill -9 java command.
note that it kills all running java processes.
Related
Why can't I run this:
process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "\"C:\\Program Files\\Gdal\\bin\\gdal\\apps\\ogr2ogr.exe\" -f \"" + "ESRI Shapefile" + "\" \"" + "C:\\Temp\\world.shp" +"\" PG:\"host=" + MainDialog.host + " user=" + MainDialog.dbUser + " password=" + MainDialog.dbPassword + " dbname=" + MainDialog.dataBase + "\" \"" + layerName + "\" -dim " + coordformat});
Which results in command:
"C:\Program Files\Gdal\bin\gdal\apps\ogr2ogr.exe" -f "ESRI Shapefile" "C:\Temp\world.shp" PG:"host=127.0.0.1 user=puser password=pwd dbname=db1" "world" -dim XY
This command can be executed in a Windows cmd-terminal without any problems...
but when running the java process nothing happens, no error codes.
It is much easier to use the String[] for exec(cmd) calls as this avoids problems with the punctuation / escape for parameter values. Try as something like this:
String [] cmd = new String[]{
"C:\\Program Files\\Gdal\\bin\\gdal\\apps\\ogr2ogr.exe"
,"-f"
,"ESRI Shapefile",
,"C:\\Temp\\world.shp"
,"PG:\"host=" + MainDialog.host + " user=" + MainDialog.dbUser + " password=" + MainDialog.dbPassword + " dbname=" + MainDialog.dataBase + "\""
, layerName
, "-dim"
, coordformat
};
Process p = Runtime.getRuntime().exec(cmd);
int rc = p.waitFor();
Runtime.exec is obsolete. Use ProcessBuilder instead. This will not only give your process more flexibility, it will also remove the need for all those extra quotation marks.
Many processes hang if they fill up their output or error buffer and no other process consumes it.
If you don’t care about the output of the process, just use inheritIO() to make sure none of it is blocked:
ProcessBuilder builder = new ProcessBuilder(
"C:\\Program Files\\Gdal\\bin\\gdal\\apps\\ogr2ogr.exe",
"-f", "ESRI Shapefile",
"C:\\Temp\\world.shp",
"PG:host=" + MainDialog.host,
"user=" + MainDialog.dbUser,
"password=" + MainDialog.dbPassword,
"dbname=" + MainDialog.dataBase,
layerName, "-dim", coordformat);
builder.inheritIO();
process = builder.start();
Notice that none of the arguments need to be quoted, and in fact must not be quoted.
If you do care about the output of the process, you can manually do some of the steps which inheritIO() does:
ProcessBuilder builder = new ProcessBuilder(
"C:\\Program Files\\Gdal\\bin\\gdal\\apps\\ogr2ogr.exe",
"-f", "ESRI Shapefile",
"C:\\Temp\\world.shp",
"PG:host=" + MainDialog.host,
"user=" + MainDialog.dbUser,
"password=" + MainDialog.dbPassword,
"dbname=" + MainDialog.dataBase,
layerName, "-dim", coordformat);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
process = builder.start();
try (InputStream processOutput = process.getInputStream()) {
// ...
}
Thanks for all the help everyone!
When using
"ogr2ogr.exe"
instead of
"C:\Program Files\Gdal\bin\gdal\apps\ogr2ogr.exe"
in the progressBuilder, it works...
It uses system path which is set to C:\Program Files\Gdal\bin\gdal\apps
The strange thing is that it worked with the old version of ogr2ogr.exe, placed in C:\Program Files\OSGeo4W64\bin.
I don't understand....
I am executing .exe program from Java program, sending string as command line argument. .exe program runs and print on console command line arguments which I read it in java through BufferedReader object. After reading it I found out that argument is not getting completely to exe.
Here is the string I am sending.
String script3 = "$tempDoc = '<QueryList><Query Id=\"0\" >';$selectNodes = $doc.SelectNodes('/QueryList/Query/Select');$invalidLog=\"\" \r\n" +
" foreach($s in $selectNodes)\r\n" +
"{$s=$s.Path -replace'\"';if($logs -contains $s){$tempDoc+='<Select Path=\"'+$s+'\">*</Select>';}else{if(!$invalidLog){$invalidLog = $s;}else {$invalidLog=$invalidLog +',' + $s;}}} $tempDoc += '</Query></QueryList>';$query = $tempDoc;$Results='<Error>EventLogNotFoundException:Unable to find '+$invalidLog+' event logs.</Error>';\r\n" +
"$slp = \"false\"}else {$Results= $tempResult};$flag=\\\"false\\\";}else{if($Results.count -eq 0){$Results +=$tempResult;}else {$tempResult.removeAt(0);$Results += $tempResult;}\r\n" +
"$startTime=$endTime;}}}}\r\n" +
"$Results+= '<EndofPoll>';\r\n" +
"$RunspacePool.Close() | Out-Null\r\n" +
" $RunspacePool.Dispose() | Out-Null \r\n" +
" $counter=$counter+1;$Results | Out-String;if($slp -eq \\\"true\\\"){sleep $SleepSeconds ; [Console]::Out.Flush();[System.GC]::Collect();invoke-command -scriptblock {[Console]::Out.Flush();[System.GC]::Collect()}}}}\"";
After reading from BufferedReader object I found it as
$tempDoc = '<QueryList><Query Id=0 >';$selectNodes = $doc.SelectNodes('/QueryList/Query/Select');$invalidLog="
foreach($s in $selectNodes)
{$s=$s.Path -replace'';if($logs-contains
Is there anything I am missing. Thanks.
I have the following code:
private static String[] createCommandLinux(String cvPath, String jsonPath, String libPath) {
List<String> command = new ArrayList<>();
command.add("java");
command.add("-cp");
command.add("'" + libPath + "/ResumeTransducer/bin/*:" +
libPath + "/GATEFiles/lib/*:" +
libPath + "/GATEFiles/bin/gate.jar:" +
libPath + "/ResumeTransducer/lib/*'");
command.add("code4goal.antony.resumeparser.ResumeParserProgram");
command.add("'" + cvPath + "'");
command.add("'" + jsonPath + "'");
String[] commandArr = new String[command.size()];
commandArr = command.toArray(commandArr);
return commandArr;
}
...
Runtime.getRuntime().exec(createCommandLinux(cvPath,jsonPath,libPath));
...
I copy the command to run on MACOS terminal and it works fine. But when run this command within java application, it can not work as terminal does. It notifies that it can not find the main class. Do you know what the issue with it on MAXOS ?
Thanks
I have a problem running a bashscript from java which needs parameters and will start an Appiumnode. So I want a physical terminal open where the node is running.
I managed to start a selenium grid with the following command:
rt.exec( new String[] { "/usr/bin/open" , "-a", "Terminal.app", "grid.sh"}) ;
whereas the script itself is:
#!/bin/bash
cd ~/../../usr/local/selenium
java -jar selenium-server-standalone-2.53.0.jar -role hub -port 4040
But for the appiumnodes, we have different devices and because of this we have different nodes.
I want to start these with a parametrized script:
#!/bin/bash
cd ~/../..
node /Applications/Appium.app/Contents/Resources/node_modules/appium/build/lib/main.js --nodeconfig $1 -p $2 -U $3 -bp $4
But I'm not able to get the nodes running in the terminals as with the grid.
I've tried many solutions found here and others but none of them with success. All the time, terminal is not opened and no node is started.
Solutions that I've tried are:
rt.exec( new String[] { "/usr/bin/open" , "-a", "Terminal.app", "NodeExecutor.sh " + filepath + " "+ Port + " " + parts[1] + " " + bp});
rt.exec( new String[] { "/bin/bash" , "-c", "/bin/bash NodeExecutor.sh " + filepath + " "+ Port + " " + parts[1] + " " + bp});
rt.exec( new String[] { "/usr/bin/open" , "-a", "Terminal.app"});
rt.exec( new String[] { "/usr/bin/osascript" , "-e", "tell application \"Terminal\" to do shell script \""+ filepath + "/NodeExecutor.sh " + filepath + " "+ Port + " " + parts[1] + " " + bp + "\" "});
All my bash scripts are also set as executable: chmod +x /PATH...
Can someone help me with this?
I try backup from remote server at java but it not work .It can't call mysqldump path .I don't know how to call mysqldump path from remote linux server.How can i do dynamically detect the path of linux server mysqldump from window?like that "\MySQL Server 5.0\bin\"?Pls anyone help me .
public class db {
public static void main (String arg[])
{
String path = "C:/thiri.sql";
String username = "devadmin";
String password = "root";
String dbname = "mydb";
String hostip="192.168.4.205";
String port="3306";
String mySQlPath = "/usr/bin/mysqldump";//sqldump path from Linux
String executeCmd = mySQlPath +" -h "+ hostip +" -P "+ port +" -u " +username + " -p" + password + " --add-drop-database -B " + dbname + " -r " + path;
Process runtimeProcess;
try {
// System.out.println(executeCmd);//this out put works in mysql shell
// runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The problem is probably to do with spaces in the pathname, and exec's inability to handle this.
(Seriously ... exec(String) does not understand the difference between a space between arguments and a space in a pathname. And it doesn't understand quoting, so adding quotes will only make matters worse. Read the javadocs!)
The solution is to use the exec(String[]) overload; i.e. split the command name and arguments your self.
String[] executeCmd = new String[]{
"mysqldump",
"-u", dbUserName",
"-p", dbPassword,
"--add-drop-database",
"-B", dbName,
"-r", path
};
How can i automatically detect the path of mysqldump.exe?
I don't think you can. If you can't handle this via the server-side user setting of the %PATH% variable, you will have to use a full pathname when invoking the command.
I don't know how to call mysqldump path form remote server for remote database backup.
You will need to find it out.
Try using
String executeCmd = "cmd /c mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;