I am trying to run my c/c++ .exe from eclipse RCP (Java API).
Code:
package com.jkt.rcp.texteditor.handlers;
import java.io.IOException;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
//import com.jkt.runner.utils.Test;
public class RecordHandler extends AbstractHandler {
private RecordingThread recordingThread;
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("inside RecordHandler...");
recordingThread = new RecordingThread();
recordingThread.start();
return null;
}
}
And code of RecordingThread.java is:
package com.jkt.rcp.texteditor.handlers;
import java.io.IOException;
public class RecordingThread extends Thread {
private String file = "C:\\workspace\\JProAcceptanceBot\\Record.exe";
public void run() {
System.out.println("inside Run()...");
try {
Process proc = Runtime.getRuntime().exec(file);
} catch (IOException e) {
System.out.println("IOException:"+e);
e.printStackTrace();
}
}
}
Actually RecordHandler.java executes after clicking a eclipse RCP button.
But as soon as I click button, c/c++ exe doesn't respond and my Java program stops responding.
Otherwise if I run this exe inside my eclipse, it runs fine.
This c/c++ exe has been made by using Eclipse CDT and Cygwin.
Please have a look into code and suggest ?
I'm not sure, but you might want to immediately start reading the inputstream of proc obtained through proc.GetInputStream(). In the documentation for Process:
All its standard io (i.e. stdin,
stdout, stderr) operations will be
redirected to the parent process
through three streams
(Process.getOutputStream(),
Process.getInputStream(),
Process.getErrorStream()). The parent
process uses these streams to feed
input to and get output from the
subprocess. Because some native
platforms only provide limited buffer
size for standard input and output
streams, failure to promptly write the
input stream or read the output stream
of the subprocess may cause the
subprocess to block, and even
deadlock.
This article on javaworld describes the same problem and explains the solution (on page 3).
Be aware of Sun bug 6468220 (also described in bug 6550942 and bug 6511002):
On Windows platform Runtime.exec(String[] cmdarray) does not pass correctly command line arguments, if one of them contains double quotes (").
Passing/Expected --> Actual
{ "ab\"c", "d\"ef" } --> { "abc def" }
{ "a b \" c", "d \" e f" } --> { "a b ", "c d", "e f " }
{ "a", "", "b" } --> { "a", "b" }
{ "\" a" } --> java.lang.IllegalArgumentException
So my question is: what is the exact command line you are trying to execute?
Related
I have a MainForm class (as you'd expect, it is a form) that has a text box on it. I also have another class called 'Application_Server' That does a load of other stuff (not just form-background related, quite a lot of network based stuff etc.).
The Application_Server class runs in it's own thread, but needs to be able to update the controls on the form, for this question, we will stick with just the textbox.
The problem is that even though I am executing the command to set the text of the textBox control via 'Invoke' I am still getting the following exception during runtime:
Additional information: Cross-thread operation not valid: Control
'DebugTextBox' accessed from a thread other than the thread it was
created on.
What could be causing this? I am definitely invoking a delegate within MainForm.
Here are the relevant code segments (cut down for readability):
MainForm.h:
public ref class MainForm : public System::Windows::Forms::Form {
delegate void del_updateDebugText(String^ msg);
del_updateDebugText^ updateDebugText = gcnew del_updateDebugText(this, &MainForm::postDebugMessage);
private: void postDebugMessage(String^ message);
};
MainForm.cpp:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
if (this->DebugTextBox->InvokeRequired)
{
this->Invoke(updateDebugText, gcnew array<Object^> { message });
}
else
{
this->DebugTextBox->AppendText(message);
}
Monitor::Exit(DebugTextBox);
}
And finally, the code calling it:
void ServerAppManager::postDebugMessage(System::String^ message)
{
mainFormHandle->updateDebugText(message);
}
void ServerAppManager::applicationStep()
{
postDebugMessage("Starting\n");
// This is Run in seperate thread in MainForm.cpp
while (s_appState == ApplicationState::RUN)
{
postDebugMessage("Testing\n");
}
}
Thanks!
From background worker called bwSearch we do the call as following from the DoWork event handler:
private: System::Void bwSearch_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
//... logic
UpdateTxtOutput("Some message");
//... more logic
}
I have a RitchTextBox called txtOutput, also the windows form control containing this code is called frmMain, the UpdateTxtOutput is defined in three parts as follows:
delegate void UpdateTxtOutputDelegate(String^ text);
void UpdateTxtOutput(String^ text)
{
UpdateTxtOutputDelegate^ action = gcnew UpdateTxtOutputDelegate(this, &frmMain::Worker);
this->BeginInvoke(action, text);
}
void Worker(String^ text)
{
txtOutput->AppendText("\t" + text + "\n");
}
I managed to get it working by simplifying the method within the 'MainForm' class to:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
DebugTextBox->AppendText(message);
Monitor::Exit(DebugTextBox);
}
And then moving the 'Invoke' call to the method calling the delegate, not pretty but it works for now. I think the issue may have been caused by the form getting stuck inside an Invoke loop. I say this as I noticed that the form would lock up and stop responding after it hit the recursive Invoke statement.
I've created a java library (named: invoke) with a java class (Invoke). Its seen when expanding Script libraries under code in the designer navigation pane.
The code is:
package com.kkm.vijay;
public class Invoke {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
Process p = r.exec("C://some.exe");
}
}
Used the following ssjs to an onclick event of a button shows Error:500 when previewed in browser.
importPackage(com.kkmsoft.vijay);
var v=new Invoke();
v.main();
Even i used a function inside the class and changed the last line of ssjs to v.fn(). Yet the same problem.
There are a number of things wrong, and as Fredrik mentions you should switch on the standard Error page.
Your first code won't run because it is not correctly capturing the Exception. You are also using a main() method, which is normally used to execute a program. But you are calling it without any arguments. Avoid using that method unless it is for executing an application.
So change it to this:
package com.kkm.vijay;
import java.io.IOException;
public class Invoke {
public void mainCode() {
Runtime r = Runtime.getRuntime();
try {
Process p = r.exec("C://WINDOWS//notepad.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}
You should put that code in the new Java view in Designer.
Next your button code needs to change.
var v=new com.kkm.vijay.Invoke();
v.mainCode();
Testing that it should work fine. The issue next is, as it is SSJS the application will execute on the server. There may be security implications in this, and it may need you to modify the java.policy file in order to do this.
The related permission will be java.io.FilePermission.
I have this main.Boot which is actually a splash screen requires to be always on top of everything. But in my case what happening is it gets lost and main.main gets the first position which even do not have any setAlwaysOnTop(true);
How can i set main.Boot always on top?
Boot.java:
package main;
public class Boot
{
public static void main(String[] args)
{
try {
String myCmd;
// Layer 2 : it can be any other third party Java applications getting launched
// here its just one example used simple another JWindow...
myCmd = "java -cp /tmp/dist/AnotherProcess.jar main.main";
Runtime.getRuntime().exec(myCmd);
System.out.println("Running: " + myCmd);
} catch(Exception e) {
System.out.println(e);
}
myTimer(); // just a timer counting 40 seconds doing nothing else
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
window = new JWindow();
....
//setFocusable(true);
window.pack();
window.setLayout(new BorderLayout());
window.setSize(screen.width, screen.height+1);
window.setLocationRelativeTo(null);
window.setAlwaysOnTop(true); // Layer 1
// (always on top) > but it gets behind
// what ever was launched using .exec(..)
window.setVisible(true);
}
}
JFrame/JWindow doesn's support Modality correctly back to the Native OS this is job for un_decorated JDialog with following two methods
setAlwaysOnTop
setModal(true)
Notice not possible (Windows OS) to block keys Atl + F4 or Ctlr + Alt + F4
It may not be supported on your platform.
From the docs:
Note: some platforms might not support always-on-top windows. To
detect if always-on-top windows are supported by the current platform,
use Toolkit.isAlwaysOnTopSupported() and isAlwaysOnTopSupported(). If
always-on-top mode isn't supported by the toolkit or for this window,
calling this method has no effect.
I plan to start my first lesson in j2me, and I download a simple book and I try my first program.
When I take a second step to add commands, I face an error message which is:
uncaught exception java/lang/noclassdeffounderror: readfile.
So, would you please help me to understand this message? and how to solve it?
Please find my code below.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ReadFile extends MIDlet implements CommandListener
{
private Form form1;
private Command Ok, Quit;
private Display display;
private TextField text1;
public void startApp()
{
form1 = new Form( "TA_Pog" );
Ok = new Command("Ok", Command.OK, 1);
Quit = new Command("Quit", Command.EXIT, 2);
form1.setCommandListener(this);
form1.addCommand(Ok);
form1.addCommand(Quit);
text1 = new TextField("Put Your Name :","His Name : " , 32, TextField.URL );
form1.append(text1);
display = Display.getDisplay(this);
display.setCurrent(form1);
}
public void commandAction(Command c , Displayable d)
{
if (c == Ok)
{
Alert a = new Alert("Alert","This Alert from Ok Button", null, AlertType.ALARM);
a.setTimeout (3000);
display.setCurrent(a,this.form1);
}
else
{
this.notifyDestroyed();
}
}
public void pauseApp() {}
public void destroyApp( boolean bool ) {}
}
Note: the code above is taken exactly from a book.
Thanks in advance
Besr regards
uncaught exception java/lang/noclassdeffounderror: readfile.
I somehow doubt the message is exactly as you describe it. Does it look more like below?
uncaught exception java/lang/NoClassDefFoundError: ReadFile
Please keep in mind in Java it matters much whether you use lower or upper case letters. As long as you don't pay attention to stuff like that, you are likely be getting a lot of problems like that.
Now, take a closer look at your class name:
public class ReadFile //...
The exception you are getting most likely says that Java machine can't find the class you try to use. There is something wrong with your build/compilation.
I run your code. It's running good. I think you have to clean and build your project. Firstly go to project properties and then go to Application Descriptor and click on Midlet tab, and select your midlet and press ok then clean build, run it.
At the J2me application I used an alert with yes, no command. If user clicks the yes command Form Screen will be displayed and if clicks the no command TextBox screen will be displayed. But the code does not work. For two command only textbox screen will be displayed.
This is my code:
public Login(){
yes=new Command("Yes",Command.OK,1);
no=new Command("No",Command.CANCEL,1);
alert=new Alert("","Save The Changes?",null,AlertType.CONFIRMATION);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(yes);
alert.addCommand(no);
textbox.setCommandListener(this);
alert.setCommanListener(this);
}
public void commandAction(Command command, Displayable displayable) {
if(displayable==textbox)
{
if(command==exit)
{
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
if(command==no)
{
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
switchDisplayable(alert,getTextbox());
}
}
}
Where is my fault?
Your main fault here is I think not using appropriate logging in your MIDlet. Other than that, there are no evident mistakes in the code snippet you posted.
It is most likely that the error is caused by something going wrong in your getForm() method code, but since there is no logging, you have to also check other possibilities like eg that command listener or no command object, or alert object has been somehow changed somewhere else in your code.
With logging like shown in example below, you could simply run your midlet in emulator and check console messages to find out whether expected code has been executed or not:
public void commandAction(Command command, Displayable displayable) {
Log.log("command: [" + command.getCommandLabel()
+ "] at screen: [" + displayable.getTitle() + "]");
if(displayable==textbox)
{
Log.log("in textbox");
if(command==exit)
{
Log.log("handle exit command");
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
Log.log("in alert");
if(command==no)
{
Log.log("handle no command");
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
Log.log("handle yes command");
switchDisplayable(alert,getTextbox());
}
}
}
//...
public class Log {
// utility class to keep logging code in one place
public static void log (String message) {
System.out.println(message);
// when debugging at real device, S.o.p above can be refactored
// - based on ideas like one used here (with Form.append):
// http://stackoverflow.com/questions/10649974
// - Another option would be to write log to RMS
// and use dedicated MIDlet to read it from there
// - If MIDlet has network connection, an option is
// to pass log messages over the network. Etc etc...
}
}