make java communicate with a C++ program [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C++ program which uses command line as its mean for IO. I don't know C++, nor do I have the program's source code. I want my java application to open the C++ program , give some input and gather the result from the C++ code. Is there a way?
UPDATE: I need to enter the input at runtime.

You can use java.lang.Runtime
For example:
public class TestRuntime {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("test.bat");
// test.bat or test.sh in linux is script with command to run (c++) program
// or direct path to application's exec
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In addition, you can read about difference between Runtime and ProcessBuilder in this topic.

Related

Java program to listen if any other java programs are running [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I Have two java programs : Demo1.java and Demo2.java
Demo2.java
public class Demo2 extends Thread{
public void run(){
while(true){
System.out.println("Demo2 is running");
}
}
}
I want to :
Run Demo2
While Demo2 is running run Demo1
Find out from Demo1 if Demo2 is running.
How should I write Demo1.java?
If I understood, you have two programs meaning two separatly threads. So you can access to the process list just like this:
Windows:
try {
Process proc = Runtime.getRuntime().exec("process.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
oStream.write("process where name='process.exe'");
String line;
while ((line = input.readLine()) != null) {
if (line.contains("process.exe"))
return true;
}
input.close();
}
catch (Exception ex) {
// handle error
}
Linux:
try {
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ps aux | grep process" });
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
if (line.contains("process")) {
// process is running
}
}
}
catch (Exception e) {
// handle error
}
Hope it helps.

How to run Microsoft access macro from Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
We are using Access database for a project along with Java. We have some Macros in Access database and we need to run those from Java. Is there any way to execute those macros from Java?
The following code works for me in NetBeans 8 on Windows 8.1. It writes a temporary VBScript file and then runs it using cscript.exe:
package runaccessmacro;
import java.io.*;
public class RunAccessMacro {
public static void main(String[] args) {
String dbFilePath = "C:\\Users\\Public\\Database1.accdb";
String vbsFilePath = System.getenv("TEMP") + "\\javaTempScriptFile.vbs";
File vbsFile = new File(vbsFilePath);
PrintWriter pw;
try {
pw = new PrintWriter(vbsFile);
pw.println("Set accessApp = CreateObject(\"Access.Application\")");
pw.println("accessApp.OpenCurrentDatabase \"" + dbFilePath + "\"");
pw.println("accessApp.DoCmd.RunMacro \"doRidLogUpdate\"");
pw.println("accessApp.CloseCurrentDatabase");
pw.println("accessApp.Quit");
pw.close();
Process p = Runtime.getRuntime().exec("cscript /nologo \"" + vbsFilePath + "\"");
p.waitFor();
BufferedReader rdr =
new BufferedReader(new InputStreamReader(p.getErrorStream()));
int errorLines = 0;
String line = rdr.readLine();
while (line != null) {
errorLines++;
System.out.println(line); // display error line(s), if any
line = rdr.readLine();
}
vbsFile.delete();
if (errorLines == 0) {
System.out.println("The operation completed successfully.");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Notes:
This will only work on a Windows machine with Microsoft Access (the actual application, not just the Access Database Engine) installed.
The "bitness" of the JVM under which the Java code runs should match the "bitness" of the version of Access installed (i.e., both 64-bit or both 32-bit).
Some tweaking may be required under certain circumstances, e.g., Java code being executed by a web server may be prohibited from shelling out to cscript.exe by default.

Running Javascript Code Used to Run on NodeJS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code that is running on NodeJs. We would like to change the technology (to java).
The problem is we have some existing passwords, and I am not sure how to I copy the encryption logic to java.
So, one of the possible solutions is to run the encryption logic in javascript (e.g. command line, embbeded in the java, etc) and get the result back.
The question is - how do I do that?
The nodejs code goes like this:
crypto = require('crypto');
this.salt = this.makeSalt();
encryptPassword: function(password) {
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, iterations, keylen).toString('base64');
crypto.randomBytes(..)
}
makeSalt: function() {
return crypto.randomBytes(numOfBytes).toString('base64');
},
UPDATE:
Following the suggestions here, I added the full code. If the right way of doing it is by transforming the javascript code to java code, can you please help me translated the above code?
You should not do this, if you want random bytes in Java do this. You should be able to replicate the encryption logic in Java.
byte[] b = new byte[20];
new Random().nextBytes(b);
Almost all of the Node.js crypto functions are generic, and should have their own Java counterparts or 3rd party libraries.
Update
If you must run your node code via java you can add this method
public static String runCommand(String command) {
String output = "";
try {
String line;
Process process = Runtime.getRuntime().exec( command );
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()) );
while ((line = reader.readLine()) != null) {
output += line;
}
reader.close();
} catch (Exception exception) {
// ...
}
return output;
}
and run it like this
String encryptedPassword = runCommand("node myEncryption.js --password=1234");

reading both stdin and arguments from command line java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble reading both arguments and stdin from the command line when running a java file. I can read in arguments on their own and stdin on it's own but not together; for example:
java myFile 6 2 < numbers.txt
I can get it to store 6 and 2 in an array but then it just stores "<" and "text.txt" also. I've been unable to find anything online describing a similar problem so not really sure where to begin.
Command-line arguments are received in the String[]-typed parameter of the main method. Input redirection is done the same as for any other process invoked at the command line. The bytes can be retrieved by reading from stdin until EOF is reached.
Command: java myClass myArg < myFile
public static void main(String[] args)
{
System.out.println("Arg 1 = " + args[0] + "\nStdin = ");
try (InputStreamReader isr = new InputStreamReader(System.in)) {
int ch;
while((ch = isr.read()) != -1)
System.out.print((char)ch);
}catch(IOException e) {
e.printStackTrace();
}
}
For more info:
http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
http://docs.oracle.com/javase/tutorial/essential/io/cl.html

Running Matlab from Java code [duplicate]

This question already has answers here:
Running MATLAB function from Java
(5 answers)
Closed 9 years ago.
sorry if I ask a simple question but I would like to ask whether it is possible when executing a java source to have code in it which makes an independent Matlab programme run (not only to execute Matlab code in java) ? I think this is also general question whether you can start other programmes in the process of execution of your code in Java.
Thank you.
Best,
M
I know you can run external Programmes like this:
import java.io.*;
public class CommandExection {
public CommandExection(String commandline) {
try {
String line;
Process p = Runtime.getRuntime().exec(commandline);
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
}
public static void main(String argv[]) {
new CommandExection("c:\\Yourprogram.exe");
}

Categories