I am trying to launch MySql server installer which is in my resources folder but it terminates after a few seconds. However if I launch it manually it runs okay until the end. Below is my code.
Thread t = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
String fileUrl = classloader.getResource("mysql.msi").getFile();
Runtime rf = Runtime.getRuntime();
Process pf = rf.exec("msiexec /i \"\\" + fileUrl + "\"");
} catch (Exception e) {
// System.out.println(e.toString()); // not necessary
e.printStackTrace();
}
}
});
t.start();
Okay, it was just an advice, lets come to your case, Windows OS has certain set of security restrictions which allows only administrator to install or remove any application.
That is why, we see a promt window asking for Administrator password (or Admin's permission as YES/NO type, in case user has logged in as admin), and the promt screen is the heart of it's security, as it don't allow ANY OTHER APPLICATION TO HAVE CONTROL ON IT.
If you do a remote desktop via third party, you will never see the client machines promt screen (this is because of security constraints), so in your case, your java application is third party app which don't have enough permission to continue the operation further.
Hence it closes after few seconds.
How ever, you can start and stop already installed services by allowing permission once in your windows service control. So I was suggesting you to play with service only.
Related
So, this is a rather unusual question, and I can't find anything else anywhere which has been helpful on how to do this, or if its even possible to do so.
I'm working on a game server wrote in java, and I'm trying to get the users default web browser to open to a specific link, when a command is typed into the chat box and sent to the server.
The current Issue I have is, when a user issues the command, it opens the browser on the host system, and not the players system.
I haven't been able to try any other methods, as I am unable to find any information regarding my specific situation!
#CommandHandlerMethod(accessLevel = EAccessLevel.USER)
public static Object[] vote(final Player player, final String... params) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("www.example.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
return AbstractCommandHandler.getAcceptResult("");
}
What I was hoping for via this code, was to open the web browser on the players system to allow them to view a specific webpage, but this has not been the case, and opens it on the server host system.
I am trying to open a static url from a web application when user clicks a button on a screen. Our application is deployed on a linux box and using the below program its trying to open a browser. Can you please advise how I can get it to to open it on the client instead ?
All our users access this application from windows.
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://www.google.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
I'm 99% sure above solution works only for windows, for unix I believe you should try something like this:
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);
I have created a Java application and used Java Service Wrapper method 3 that implements the WrapperListener to convert it to a Windows Service.
The WrapperManager can be used to identify various events that get raised and caught in the wrapper listener controlEvent function, this includes a user logging off, however doesn't have a user login event.
Is there a way I can detect a user logging in to the system and perform some kind of action in my Java application?
If not, is there a way to detect a spike in CPU usage that I can use to make an assumption of a user logging in?
This is required because my application needs to perform some action when a user logs in.
There is no logon control code for user logons within the Service Control Manager. Additionally Java Service Wrapper makes use of common control signals. It would not make much sense to have a CTRL_LOGON_EVENT when working with event handling: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683242.aspx
You might need to consider looking around in the Windows Event Log for event id 4648 under Security and programming an implementation of event subscription but it will require an understanding of Windows APIs and the usage of the Java Native Access library:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385771.aspx
The workaround above is not ideal but it's a possible route.
A bit more information about the service you're working on would really help.
EDIT:
On user login, this program should run as the user, (refer at bottom for the command):
LoginServiceDefer.java
import org.tanukisoftware.wrapper.WrapperManager;
public class LoginServiceDefer {
public static String svc = "loginservice";
public static int ctrl = 155;
public static void main(String[] args) {
System.out.println("Sending user control code.");
try {
WrapperManager.sendServiceControlCode(svc, ctrl);
WrapperManager.stop(0);
} catch (Exception re) {
System.err.println("System error. Unable to send control code to service, " + svc + ", with control code, " + ctrl + ".");
re.printStackTrace();
WrapperManager.stop(1);
}
}
}
This is the listener that will be used in your service. It runs as SYSTEM:
LoginListener.java
import org.tanukisoftware.wrapper.event.WrapperEvent;
import org.tanukisoftware.wrapper.event.WrapperEventListener;
import org.tanukisoftware.wrapper.event.WrapperServiceControlEvent;
public class LoginListener implements WrapperEventListener
{
public LoginListener() { }
public void fired( WrapperEvent event ) {
if (event instanceof WrapperServiceControlEvent) {
WrapperServiceControlEvent scEvent = (WrapperServiceControlEvent) event;
switch (scEvent.getServiceControlCode()) {
case 155:
// LoginServiceDefer has sent a control code.
break;
}
}
}
}
Add this line before WrapperManager.start()
WrapperManager.addWrapperEventListener(new LoginListener(), WrapperEventListener.EVENT_FLAG_SERVICE);
Add these two lines to your wrapper.conf
wrapper.java.additional.1=-Djava.security.manager
wrapper.java.additional.2=-Djava.security.policy=java.policy
Create a new file called java.policy.
Place it in the folder where Wrapper.exe is located (should be wrapper-windows/bin/wrapper.exe).
// NOTE: There are ways of limiting the permissions for the notifier using the the library.
grant codeBase "file:../lib/wrapper.jar" {
permission java.security.AllPermission;
};
// Change *my_login_notifier.jar* to whatever LoginServiceDefer.java is as a jar.
grant codeBase "file:./*my_login_notifier.jar*" {
permission java.security.AllPermission;
};
Run this command on user login, making sure to cd to the path or edit the java.policy file:
java -Djava.security.manager -Djava.security.policy=java.policy -jar *my_login_notifier.jar*.jar
there is another solution called JavaExe. With this tool you can receive some System events into your Java application, like User Connect or Disconnect, ...
I am finding out a way to switch off my tablet automatically.
Right now when we long press power button, I get a confirmation for shutdown (Cancel or Ok).
Is there a way to programmatically switch off the device without confirmation ?
Is this possible?
No. Suitably rooted phones/tablets often have access to su/reboot commands, but for an off-the-shelf, commercially available device, no: there is no way to programatically shut it down.
This is a dicey one! As an app, you cant do much, but there is one way you can try this. Get a phone which is rooted and grants your application SuperUser permissions. Then you could try to run this piece of code from your APK.
Process mProcess = null;
DataOutputStream osStream;
try {
mProcess = Runtime.getRuntime().exec("su");
} catch (IOException e) {
Log.e("Error","Unable to get SU permissions, quitting");
}
osStream = new DataOutputStream(mProcess.getOutputStream());
try {
osStream.writeBytes("reboot");
Thread.sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Not a tested piece of code, but hopefully should give you some idea!
Although you can't really invoke the shutdown programmatically in non-rooted device, there's a way how to disable the confirmation dialog that occurs when you long-press the power button. There's a secret code
*#*#7594#*#*
or
*#7594#
which changes the power button behaviour - enables direct power off once the code enabled. You need to choose this code via default dialpad. Works on most Android phones.
Here's the list of some other secret codes.
I am doing a networking project. I compiled a code under Java Project console app and it works. But when I create a GUI and assign the code to run when a button is pressed, it hangs on clicking the button.
This is the source code:
#Action
public void EstablishConnection() {
serverAddress = jTextFieldServerAddress.getText();
serverPort = Integer.parseInt(jTextFieldPort.getText());
serverUName = jTextFieldUName.getText();
serverUPwd = jTextFieldUPwd.getText();
try {
client = new FTPClient();
client.connect(serverAddress, serverPort);
boolean login = client.login(serverUName, serverUPwd);
if(login) {
System.out.println("Successfully logged in\n");
}
else {
System.out.println("Unable to login\n");
}
}
catch(Exception ex) {
System.out.println("Exception Raised: " + ex);
}
}
The action is called when a button is pressed in the swing app. It is not working for me. But it is working very fast for a console app.
Anytime I see the word "hang" I assume you need to be using a separate Thread to execute the hanging code. See Concurrency in Swing for the solution.
I would suggest that you should run code that depends on external factors, like accessing a remote server etc., that could delay the response, in a thread of it's own.
Display a MessageDialog with an indeterminate progress bar:
connProgressBar.setIndeterminate(true);
You neither know whether your connection will terminate, nor if it will, so add a button that allows the user to kill the connection thread, whenever she feels like it.
Since you are probably connecting to an ftp server in order to upload and download files, after the connection has been established, use a determinate progressbar that shows the download percentage of the file or files progress, that runs in a new thread.