I try to launch a java program in a eclipse plugin, but failed.
What I try to do is to let the example from "Contribution to eclipse" to work.
In the example, the plugin contribute an menu item to eclipse,
when you select a test class, you can use the menu to run test cases in the class.
To run the test case, there are two class TestRunner and SocketTestRunner.
TestRunner launch the SocketTestRunner as a seperate java program, and communicate
through socket.
Code of TestRunner related to launch java program:
final String MAIN_CLASS = "org.eclipse.contribution.junit.SocketTestRunner";
public TestRunner() {
}
public void run(IType type) throws CoreException {
project = type.getJavaProject();
run(new IType[] { type });
}
public void run(IType[] classes) throws CoreException {
IVMInstall vmInstall = JavaRuntime.getVMInstall(project);
if (vmInstall == null)
vmInstall = JavaRuntime.getDefaultVMInstall();
if (vmInstall == null)
return;
IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
if (vmRunner == null)
return;
String[] classPath = computeClasspath();
// prepare arguments:
// Argument[0] = port
// Argument[1-n] = types
port = SocketUtil.findFreePort();
String[] args = new String[classes.length + 1];
args[0] = Integer.toString(port);
for (int i = 0; i < classes.length; i++) {
args[1 + i] = classes[i].getFullyQualifiedName();
}
VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(MAIN_CLASS,
classPath);
vmConfig.setProgramArguments(args);
ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
vmRunner.run(vmConfig, launch, null);
connect();
}
Code of SocketTestRunner:
public static void main(String[] args) {
try {
logger = new PrintWriter(new FileWriter("log.txt"), true);
logger.println("main");
for (int i = 0; i < args.length; i++) {
logger.println(args[i]);
}
new SocketTestRunner().runTests(args);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (logger != null) {
logger.close();
}
}
}
private void runTests(String[] args) {
port = Integer.parseInt(args[0]);
logger.println("port: " + port);
openClientSocket();
try {
logger.println("Befor new TestSuite");
TestSuite suite = new TestSuite();
for (int i = 1; i < args.length; i++) {
logger.println("add TestSuite");
suite.addTestSuite(Class.forName(args[i]).asSubclass(TestCase.class));
}
logger.println("starting tests " + suite.countTestCases());
//writer.println("starting tests " + suite.countTestCases());
logger.println("Before TestResult");
TestResult result = new TestResult();
logger.println("After TestResult");
result.addListener(this);
suite.run(result);
//writer.println("ending tests ");
logger.println("ending tests ");
} catch (Exception e) {
logger.println("exception: " + e.getMessage());
logger.println(e.toString());
e.printStackTrace(logger);
e.printStackTrace();
} finally {
closeClientSocket();
}
}
private void closeClientSocket() {
logger.println("closeClientSocket");
if (writer != null) {
writer.close();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void openClientSocket() {
logger.println("openClientSocket");
try {
socket = new Socket("localhost", port);
writer = new PrintWriter(socket.getOutputStream(), true);
return;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
When I test in eclipse(3.6.0 HELIOS), I create a simple JUnit test case in runtime eclipse,
select the test class and click the menu item in context menu,
then the runtime eclipse will not response any further interaction,
let the ServerSocket still keep listening, and no test log I add for debug.
And I can get the command line which eclipse used to launch the SocketTestRunner:
"C:\Program Files\Java\jre6\bin\javaw.exe" -classpath C:\Users\User\workspace\org.eclipse.contribution.junit\bin;C:\Users\User\workspace\org.eclipse.contribution.junit\plugins\org.eclipse.contribution.junit_1.0.0.201009102354.jar;C:\Users\User\runtime-EclipseApplication\Hello\bin;C:\Users\User\workspace\org.junit\junit.jar;"C:\Program Files\eclipse\plugins\org.hamcrest.core_1.1.0.v20090501071000.jar" org.eclipse.contribution.junit.SocketTestRunner 41872 org.eclipse.contribution.hello.HelloTest
When I use this command manually, I can run the test case succeed(after comments the socket related writer), as I can get test log.
main
41872
org.eclipse.contribution.hello.HelloTest
port: 41872
openClientSocket
Befor new TestSuite
add TestSuite
starting tests 1
Before TestResult
After TestResult
starting test testSayHi(org.eclipse.contribution.hello.HelloTest)
failing test testSayHi(org.eclipse.contribution.hello.HelloTest)
END TRACE
ending tests
closeClientSocket
Any advise will be appreciated, Thanks.
Finally I get this plugin to run.
You should set working directory.
vmConfig.setWorkingDirectory("C:\\Users\\User");
When I try to get working directory, I get null.
You can't set breakpoint at the line which invoke connect() function.
private void connect() {
try {
ServerSocket server = new ServerSocket(port);
try {
Socket socket = server.accept();
try {
readMessage(socket);
} finally {
socket.close();
}
} finally {
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
But I don't know why, any advise will be appreciated.
You can, but the problem arise just before the connect method is called. So the plugin hooks before connect is called waiting for SocketTestRunner is launched.
Related
#FXML
private TextArea textarea;
#FXML
private ImageView imagev;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Serverth Server = new Serverth();
Server.start();
}
class Serverth extends Thread {
#Override
public void run() {
try {
final int NUM_THREAD = 99;
ServerSocket socket = new ServerSocket(8078);
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREAD);
System.out.println("SERVER SOCKET CREATED");
while (!isInterrupted()) {
Socket in = socket.accept();
Runnable r = new ThreadedHandler(in);
exec.execute(r);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
class ThreadedHandler implements Runnable {
private Socket incoming;
public ThreadedHandler(Socket in) {
incoming = in;
}
public void run() {
try {
try {
ObjectInputStream is=new ObjectInputStream(incoming.getInputStream());
while(true) {
if (is.available() > 0) {
String line = is.readUTF();
textarea.appendText("\n" + "[" + new java.util.Date() + "] : " + line);
if (line.contains("inviato")) {
Object obj = is.readObject();
Email ema = (Email) obj;
try {
SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy-hh-mm-ss");
FileOutputStream fileOut = new FileOutputStream("src/Server/" + ((Email) obj).getDestinat() + "/" + formatter.format(((Email) obj).getData()) + ".txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(ema);
objectOut.flush();
objectOut.close();
System.out.println("The Object was succesfully written to a file");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
} catch(IOException ex) {
ex.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
incoming.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Inside the run method (in Serveth class), I create a server socket and call exec.execute method.
Inside the run method (in ThreadedHandler class), the server is waiting for messages from the client (in this specific case, it creates a new .txt file but it is not important).
Everything works but causes excessive use of the CPU and lag!!!
InputSteam.available method returns a value instantly, telling you no bytes are available to be read, so this code runs a very "hot" spin loop:
while(true) {
if (is.available() > 0) {
...
}
}
The available method is rarely useful and often gives confusing results (see for example inputstream.available() is 0 always). I would suggest you get rid of the if statement altogether:
while(true) {
String line = is.readUTF();
textarea.appendText("\n" + "[" + new java.util.Date() + "] : " + line);
...
}
There's no way for this your code to exit the loop normally. You may want to add a mechanism for the client to disconnect from the server.
I've searched quite a bit for answers, but haven't found anything that really helps, and the threads I found were zombies.
My problem: I'm creating a UI for Bukkit servers, but for end-users to execute commands in the server, I have to write to the process using an OutputStreamWriter.
I have all the code, but the command isn't getting sent to the process.
So far, this is the code I'm using to execute the server and get all the streams:
#SuppressWarnings("null")
private void startServer() throws InterruptedException, IOException {
setTitleText("BukkitUI by Beatsleigher [Starting...]");
setStatusImg(ServerState.BOOTING);
Thread.sleep(1000);
setTitleText("BukkitUI by Beatsleigher [Loading components...]");
runServer = true;
process = new ProcessBuilder();
// Set attibutes
process.redirectErrorStream(true);
java.util.List<String> jvmArgs = new ArrayList<>(4);
jvmArgs.add(JVMManager.getJava().toString()); // If you don't know what this does, you should not consider yourself a programmer.
jvmArgs.add("-jar");
jvmArgs.add(JVMManager.getProcessedInitHeapSize()); // Add Java VM arg[0]
//jvmArgs.add(JVMManager.getProcessedMaxHeapSize()); // Add Java VM arg[1]
jvmArgs.add(PreferenceManager.getBukkitLocation().getAbsolutePath()); // Add Java VM arg[2]
jvmArgs.add("-o"); // Add CraftBukkit arg[0]
jvmArgs.add("true"); // Add CraftBukkit arg[1]
process.command(jvmArgs);
// Start process
pr = process.start();
setTitleText("BukkitUI by Beatsleigher [Running]");
consoleWriter = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
processServer();
}
private void processServer() throws IOException {
new Thread() {
#Override
public void run() {
try {
setStatusImg(ServerState.ONLINE);
jTextPane1.setText("");
consoleReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((consoleOutput = consoleReader.readLine()) != null && (runServer)) {
jTextPane1.setText(jTextPane1.getText() + "\n" + consoleOutput);
highlight();
}
jTextPane1.setText(jTextPane1.getText() + "\n[INFO] [BukkitUI] Server has Stopped!");
highlight();
stopServer();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
pr.destroy();
try {
consoleReader.close();
} catch (IOException ex1) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
}
setStatusImg(ServerState.OFFLINE);
setTitleText("BukkitUI by Beatsleigher [ERROR! Server has stopped!]");
}
}
}.start();
}
The server starts and runs fine, I get all the process output I need, but, as mentioned before, any commands I send to the console using following code won't work.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String inputCmd = jTextField1.getText();
try {
if (inputCmd != null | !inputCmd.equals("")) {
if (inputCmd.startsWith("/")) {
String[] cmd = inputCmd.split("/");
consoleWriter.write(cmd[1]);
clearCmd();
return;
} else if (inputCmd.startsWith("cmd;")) {
String[] cmd = inputCmd.split("cmd;");
consoleWriter.write(cmd[1]);
clearCmd();
return;
}
if (inputCmd.startsWith("bcast;")) {
String[] msg = inputCmd.split("bcast;");
consoleWriter.write("broadcast " + msg[1]);
clearCmd();
return;
}
if (inputCmd.startsWith("appnd;")) {
String[] msg = inputCmd.split("appnd;");
jTextPane1.setText(jTextPane1.getText() + "\n[BUKKITUI] " + msg[1]);
clearCmd();
return;
}
if (inputCmd.equals("--h")) { showHelp(); clearCmd(); return; }
consoleWriter.write("say " + inputCmd);
clearCmd();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Any help with this matter is greatly appreciated.
I'm trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write su and press enter, so write:
screenrecord --time-limit 10 /sdcard/MyVideo.mp4
and press again enter and start the recording of the screen using the new function of android kitkat.
so, i try to execute the same code from java using this:
Process su = Runtime.getRuntime().exec("su");
Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");
But don't work because the file is not created. obviously i'm running on a rooted device with android kitkat installed. where is the problem? how can i solve? because from terminal emulator works and in Java not?
You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.
Try something like this:
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
A modification of the code by #CarloCannas:
public static void sudo(String...strings) {
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
for (String s : strings) {
outputStream.writeBytes(s+"\n");
outputStream.flush();
}
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
(You are welcome to find a better place for outputStream.close())
Usage example:
private static void suMkdirs(String path) {
if (!new File(path).isDirectory()) {
sudo("mkdir -p "+path);
}
}
Update:
To get the result (the output to stdout), use:
public static String sudoForResult(String...strings) {
String res = "";
DataOutputStream outputStream = null;
InputStream response = null;
try{
Process su = Runtime.getRuntime().exec("su");
outputStream = new DataOutputStream(su.getOutputStream());
response = su.getInputStream();
for (String s : strings) {
outputStream.writeBytes(s+"\n");
outputStream.flush();
}
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
res = readFully(response);
} catch (IOException e){
e.printStackTrace();
} finally {
Closer.closeSilently(outputStream, response);
}
return res;
}
public static String readFully(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return baos.toString("UTF-8");
}
The utility to silently close a number of Closeables (SoŃket may be no Closeable) is:
public class Closer {
// closeAll()
public static void closeSilently(Object... xs) {
// Note: on Android API levels prior to 19 Socket does not implement Closeable
for (Object x : xs) {
if (x != null) {
try {
Log.d("closing: "+x);
if (x instanceof Closeable) {
((Closeable)x).close();
} else if (x instanceof Socket) {
((Socket)x).close();
} else if (x instanceof DatagramSocket) {
((DatagramSocket)x).close();
} else {
Log.d("cannot close: "+x);
throw new RuntimeException("cannot close "+x);
}
} catch (Throwable e) {
Log.x(e);
}
}
}
}
}
Process p;
StringBuffer output = new StringBuffer();
try {
p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
String response = output.toString();
return response;
Late reply, but it will benefit someone. You can use the sh command in the exec() method.
Here is my example:
try {
File workingDirectory = new File(getApplicationContext().getFilesDir().getPath());
Process shProcess = Runtime.getRuntime().exec("sh", null, workingDirectory);
try{
PrintWriter outputExec = new PrintWriter(new OutputStreamWriter(shProcess.getOutputStream()));
outputExec.println("PATH=$PATH:/data/data/com.bokili.server.nginx/files;export LD_LIBRARY_PATH=/data/data/com.bokili.server.nginx/files;nginx;exit;");
outputExec.flush();
} catch(Exception ignored){ }
shProcess.waitFor();
} catch (IOException ignored) {
} catch (InterruptedException e) {
try{ Thread.currentThread().interrupt(); }catch(Exception ignored){}
} catch (Exception ignored) { }
What have I done with this?
First I call the shell, then I change (set) the necessary environments in it, and finally I start my nginx with it.
This works on unrooted devices too.
Greetings.
I am trying to execute a c++ code from java on a remote Windows machine. In order to deal with the remote part, I have created a Web service from where the actual command is run using Runtime.exec(). The c++ exe is not being called directly from the java code. I have a batch file that eventually calls the exe.
The problem is, both java and c++ processes hang. The java code on server side does handle the output stream and error stream. Also, the c++ code is logging everything in a file on Windows. The strange thing is that, when I remove the WS call and run the java code on server side as a standalone java program, it succeeds. Here is the java code:
public class RunCPlusPlusExecutable {
public int runExecutable() {
int exitValue = 0;
try {
Process p = null;
Runtime rt = Runtime.getRuntime();
System.out.println("About to execute" + this + rt);
p = rt.exec("c:/temp/execcplusplus.bat");
System.out.println("Process HashCode=" + p.hashCode());
StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
errorHandler.start();
outputHandler.start();
exitValue = p.waitFor();
System.out.println("Exit value : " + exitValue);
if (exitValue == 0)
System.out.println("SUCCESS");
else
System.out.println("FAILURE");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
}
return exitValue;
}
class StreamProcessor extends Thread {
private InputStream is = null;
private String type = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private FileWriter writer = null;
private BufferedWriter out = null;
StreamProcessor(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
writer = new FileWriter("*******path to log file********");
out = new BufferedWriter(writer);
String line = null;
while ((line = br.readLine()) != null) {
Date date = new Date();
out.write("[" + type + "]: " + date + " : " + line);
out.newLine();
}
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (out != null)
out.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Any idea what is causing the problem and how to debug it? Please note that I won't be able to debug the c++ code.
Thanks
Update 1:
Here are some more details...
The WS server is running from some admin user. And I have been running the standalone java program from some other user.
*It seems that the c++ executable is giving referenced memory error while executing from WS call. There are pop-ups citing the error with OK and Cancel buttons. *
Update 2:
The tomcat server where the WS is deployed is running as a Windows NT service. Can that be the cause of the error? If yes, how to resolve this?
I'm trying to enter some value in external application using Java.
Java application looks like this:
Runtime runtime = Runtime.getRuntime();
// ... str build ...
proc = runtime.exec(str);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
bw.write(value);
bw.flush();
bw.close();
if (proc.waitFor() != 0)
// error msg
// the end
Application hangs at waitFor method.
External application looks like this:
welcome banner
please enter 8 character input:
Welcome banner is printed using printf and input is taken with SetConsoleMode/ReadConsoleInput. ReadConsoleInput reads one char and they are masked with * character.
Help
you can use:
proc.getOutputStream().write("some date".getBytes())
keep in mind that you HAVE to read everything the app send to stdout and stderr, else it might get stuck writing there.
I use a generic class to read it in a different thread.
usage is like:
InputStreamSucker inSucker = new InputStreamSucker(proc.getInputStream());
InputStreamSucker errSucker = new InputStreamSucker(proc.getErrorStream());
proc.waitFor();
int exit = process.exitValue();
inSucker.join();
errSucker.join();
InputStreamSucker code is here:
public class InputStreamSucker extends Thread
{
static Logger logger = Logger.getLogger(InputStreamSucker.class);
private final BufferedInputStream m_in;
private final ByteArrayOutputStream m_out;
private final File m_outFile;
public InputStreamSucker(InputStream in) throws FileNotFoundException
{
this(in, null);
}
public InputStreamSucker(InputStream in, File outFile) throws FileNotFoundException
{
m_in = new BufferedInputStream(in, 4096);
m_outFile = outFile;
m_out = new ByteArrayOutputStream();
start();
}
#Override
public void run()
{
try
{
int c;
while ((c = m_in.read()) != -1)
{
m_out.write(c);
}
}
catch (IOException e)
{
logger.error("Error pumping stream", e);
}
finally
{
if (m_in != null)
{
try
{
m_in.close();
}
catch (IOException e)
{
}
}
try
{
m_out.close();
}
catch (IOException e)
{
logger.error("Error closing out stream", e);
}
if (m_outFile != null)
{
byte data[] = m_out.toByteArray();
if (data.length > 0)
{
FileOutputStream fo = null;
try
{
fo = new FileOutputStream(m_outFile);
fo.write(data);
}
catch (IOException e)
{
logger.error("Error writing " + m_outFile);
}
finally
{
try
{
if (fo != null) fo.close();
}
catch (IOException e)
{
logger.error("Error closing " + m_outFile);
}
}
}
}
}
}
public String getOutput()
{
return new String(m_out.toByteArray());
}
}
Got the answer! The trick is to use WriteConsoleInput() API because program expects keyboard event, not text ... That's why the waitFor() waited forever! :)