I have a code that uses jDesktop to open a windows explorer interface when I clicked the button LOGIN and it's working right..
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Desktop desktop = Desktop.getDesktop();
File dirToOpen;
try {
dirToOpen = new File("C://as//2010-0000-1");
desktop.open(dirToOpen);
} catch (IOException ex) {
ex.getMessage();
} catch (IllegalArgumentException iae) {
System.out.println("File Not Found");
}
}
then now, my problem is when I click the button LOGOUT, the jDesktop windows explorer interface should also be closed... I dont know what codes to use....
This is not so simple, they only chance you have is if you have a reference to the process in question. This is going to mean you're going to need to take more control over the process...This also means that it will only work on Windows...
I use the following code to show a specified file in Windows Explorer...
String path = file.getCanonicalPath();
ProcessBuilder pb = new ProcessBuilder("explorer.exe", "/select," + path);
pb.redirectError();
Process proc = pb.start();
Once you have access to the Process, you can try using Process#destory to try and terminate the process.
Launching the process should be done from a separate thread, so you don't get yourself all tied up in a block point, you should also consume the Process's output just incase it causes the process to stall.
ps- I don't have access to a Windows machine at the moment, so I'm not sure if Process#destory will work ;)
Related
I have a GUI application written in JavaFX 14 (not the XML one). I currently have three lists that are connected each to a different custom class, in whom exists an ObservableList which updates the ListViews. I have three. Upon doubleclicking on an element of the first ListView, which also contains URLs, I want to open said URL inside the default browser, no matter the OS (I'm programming on Ubuntu 20.04, and this app will be used on W10 too).
listSquads.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount()==2) {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI(bot.getSquadHandler().extractURL(listSquads.getSelectionModel().getSelectedItem())));
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
});
This is the code. The bot is a custom TwitchBot object, based on pIRCbot libraries. The squadHandler mentioned in the getter method is said custom class. Extract URL is a method that, as the name would suggest, extracts the exact URL given a string as input - this part works, as it's called upon in other parts of the code and works correctly, and I've also tested it.
However, upon double clicking on any given element of the ViewList, the app freezes to then crash a few seconds later. Why could that be? Is there a way to open the browser no matter the OS?
Thanks in advance! :)
I wanna develop apps that can compile a java code from editText. So the user write code in editText and if i click the button, programm will compile the text from edit text and show the result in textView. i found the app similar i mean such as AIDE. is there anyone can help me solution to build apps like this?
How about saving user's code somewhere in the device as .java file and then running shell command for compiling file and processing user's code.
refere below code
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);
}
Not providing required shell commands for the purpose as they would vary language to language but easy to explore.
Other option could be to call some online service that can do the compilation and provide the result.
I use a java program for communication between a arduino board and a scratch file. The communication happen well. I use a user interface to start the communication where i have buttons called
connect
close and minimize
When the user clicks the connect button code will check the value in combo box and accordingly it opens the scratch file.
Once the connect button is clicked the control moves to the scratch application. After completing my work when i tried closing the scratch. My scratch application closes as expected but the control does not return to the user interface because of which i am not able to close the application and i close it in net beans forcefully. In the output screen i don't see build successful and instead i get build stopped. That is my process works perfectly until i give connect but once the button is pressed it is hanged up some where.
I tried making it as a jar file and running it in a different machine at that time i use end task in task manager to close the application.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if("Disconnect".equals(jButton1.getText()))
{
System.exit(0);
}
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one port");
}
else
{
Runtime r = Runtime.getRuntime();
try {
//this.hide();
//p = r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");
p = Runtime.getRuntime().exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\scratch files new.sb2");
//Runtime.getRuntime().exec("taskkill /F /IM <p>.exe");
//p.destroy();
//r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
A4S a4sObj = new A4S(new String[] {jComboBox2.getSelectedItem().toString()}); //defaultline
//A4S a4sObj = new A4S(new String[]{"COM16"}); //addedline
//r.gc();
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
//p.destroy();
//System.gc();
// }
}
Here is the code i tried. But none seems to work.
Move all Process related work into separate Thread.
Use waitFor method to recognise Process end - then you are free to
exit your app.
As I can understood you used SWING for creating UI.
You can set
yourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
for your frame. This must help.
recently I have discovered substance. When i try to load it in my java program (not applet!) I get errors during startup.
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
try
{
UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
And those are the errors (Note: Those are pop ups, where --message-- is the title, and {name} are buttons):
--APPLET ALERT-- The applet is attempting to connect to jar: [...]/substance-6.0/org/pushingpixel/substance/api/skin/graphite.colorschemes. {Allow} {Disallow} {Stop Applet}
--APPLET ALERT-- The applet is attempting to invoke the java/lang/System.getenv() operatoin on KDE_FULL_SESSION {Allow} {Disallow} {Stop Applet}
The last message appears 5 times whilst writing the following into stderr
-->> returning Frame NULL
BaseDialog: owner frame is a java.awt.Frame
Also my first window will be loaded with the default swing ui.
After closing this and opening a new one (programmatically) the ui will be initialized.
Is there any way I can bypass the error?
Edit: I use the following libraries: laf-plugin-7.2; laf-widget-7.0; substance-6.0
I downloaded a fork from: https://github.com/Insubstantial/insubstantial/downloads
The other one was not signed because i compiled it myself.
I´m creating a small app in Java that needs to be used in Mac and Windows from a CD.
The basic idea of this app is just to have a main menu (different for Mac and Windows) where you can select several options (install an app, view the content of the CD, view the help manual...etc) with a the logo of a company...etc.
The app to be installed is going to be different in Windows and Mac.
What I want to do is launch the external installer and once is installed, i want to launch the app.
The main problem that i have is that once I've launched the installer in a different process, the waitfor() return a valid exitvalue and continues.
I want to wait until this app is totally installed before i try to run it.
for Windows
Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\"");
for Mac
File instFolder = new File(System.getProperty("user.dir") + "ExternalAppforMac.pkg")
Process p = Runtime.getRuntime().exec(new String[] { "open", instFolder.toString() });
int exitVal = p.waitFor();
if (exitVal==0)
...
Could you help me?
Thanks.
It seems that you need to check for the presence of the install window on the system rather than the executable. As far as I know, there is no system independent way to do this in Java, however with the use of powerful libraries like sun's JNA(which is supported on both windows and mac and can be found here) you can do this through the appropriate OS API calls.
here is an example of what you may want to do on windows, mac calls should be similiar:
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
.
.
.
//execute process
Process p = Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\"");
//wait for return value
int res = p.waitFor();
//if we have a valid return code begin waiting for window to be closed
if(res == 0)
{
//define a window handle variable
WinDef.HWND windowHandle = null;
do
{
//sleep a little while before polling the value
try{Thread.sleep(100);}catch(InterruptedException e){}
//try to fetch the window by title
windowHandle = User32.INSTANCE.FindWindow(null, "<Window Title>");
//if the handle is not null, the window is still open so sleep and then try try again
}while(windowHandle != null && windowHandle.getPointer() != Pointer.NULL);
//continue on with your code
}