I am writing a utility to start and stop windows services. The program will be distributed across many computers with differing levels of user privileges so I don't want to use the command line. I've tried using JNA,
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* #author
*/
public class WindowsServices {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try
{
// TODO code application logic here
W32ServiceManager serviceManager = new W32ServiceManager();
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SERVICE_ACCEPT_STOP);
service.stopService();
service.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
When I run the program I get the following error
com.sun.jna.platform.win32.Win32Exception: The handle is invalid.
at com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77)
at windowsservices.WindowsServices.main(WindowsServices.java:26)
Any suggestions would be most helpful.
Thanks for the suggestion the author of the question found the error.
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* #author
*/
public class WindowsServices {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try
{
W32ServiceManager serviceManager = new W32ServiceManager();
serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SC_MANAGER_ALL_ACCESS);
service.startService();
service.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
The error was that the code didn't open the Service Control Manager. I was looking on MSDN and found the process that I needed to follow. I also chanced the permission value, that might also of caused a failure.
We use Runtime.getRuntime().exec(command) and then execute the command
cmd /c net start
to start services and
cmd /c net stop
to stop services.
Of course you have to know the service names (and in our case it is DB2 we are after). But this has worked for us.
Related
I've been looking everywhere and have not found any good documentation on how to properly set up a proxy to run a script on Sauce labs and extract the HAR file. I'm using BMP in an embedded mode https://github.com/lightbody/browsermob-proxy#using-with-selenium, along with https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy. I found Sauce's documentation on setting up and running scripts through a BMP manually https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy+with+an+Additional+Proxy+Setup, however their documentation does not show how to set it up in an embedded mode only via standalone mode. Here is my setup:
My PAC file
function FindProxyForURL(url, host) {
if (shExpMatch(host, "*.miso.saucelabs.com") ||
shExpMatch(host, "saucelabs.com")) {
// KGP and REST connections. Another proxy can also be specified.
return "DIRECT";
}
// Test HTTP traffic, route it through the local BrowserMob proxy.
return "PROXY localhost:9091";
}
BMP Set up
package com.grainger.Framework;
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.grainger.Automation.Utilities;
import com.grainger.Build.BuildVariables;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;
public class BrowserMobProxyImpl {
public static Logger log = Logger.getLogger(BrowserMobProxyImpl.class.getName());
private static BrowserMobProxy MOB_PROXY_SERVER;
private static Proxy SELENIUM_PROXY;
/**
* #author xsxg091
* #return
*/
public static void startBrowserMobProxyServer(){
// start the proxy
MOB_PROXY_SERVER = getProxyServer();
// get the Selenium proxy object
SELENIUM_PROXY = getSeleniumProxy(MOB_PROXY_SERVER);
}
/**
* #author xsxg091
* #return
*/
public static BrowserMobProxy getProxyServer() {
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9090);
return proxy;
}
/**
* #author xsxg091
* #param proxyServer
* #return
*/
public static Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);;
try {
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + Integer.toString(9091));
seleniumProxy.setSslProxy(hostIp + ":" + Integer.toString(9091));
seleniumProxy.setAutodetect(false);
} catch (UnknownHostException e) {
log.error("Error initializing Selenium Proxy");
}
return seleniumProxy;
}
/**
* #author xsxg091
* #param tcName
* #param capabilities
*/
public static void setSeleniumProxy(DesiredCapabilities capabilities){
if(BuildVariables.amICapturingNetworkTraffic()){
capabilities.setCapability(CapabilityType.PROXY, SELENIUM_PROXY);
}
}
/**
* #author xsxg091
* #param tcName
* #param capabilities
*/
public static void stopBrowserMobProxyServer(){
MOB_PROXY_SERVER.stop();
}
/**
* #author xsxg091
* #return
*/
public static void getHarFile(String fileName) {
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
MOB_PROXY_SERVER.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
MOB_PROXY_SERVER.newHar(fileName);
try {
// get the HAR data
Har pageHarFile = MOB_PROXY_SERVER.getHar();
File harFile = new File(Utilities.getWorkSpace()+"//"+fileName+".har");
pageHarFile.writeTo(harFile);
} catch (IOException e) {
log.error("Unable to store Har File");
}
}
}
Here is the command I use to kick off my Sauce Tunnel
bin/sc -u ****** -k *********** -i Tunnel_Testing -v --pac file:///<path-to-pac-file>/BrowserMobProxy/browserMob.js
When I run lsof, I can port 9090 is actively listening, but I don't see 9091 in embedded mode. However, when I run it in standalone mode I can see both ports and everything works perfectly on Sauce labs. I see this when I run in embedded mode:
What am I doing wrong? Any help would be greatly appreciated. If anything is unclear, please let me know!
Thanks in advance.
I figured it out. Turns out it was a bug in version 2.1.4. When I upgraded to version 2.1.5, everything worked as it is supposed to.
Hi I am using Netbeans as my IDE. I want to play a little bit with the serial port. I am using an FTDI cable in my laptop that converts usb port to RS232 serial port.
I have found these interesting sites:
http://www.embeddedunveiled.com/
https://github.com/RishiGupta12/serial-communication-manager
I have written this piece of code taken from first link
under
Example usage
•How to find serial ports available on system is here.
code:
package findserialports;
import com.embeddedunveiled.serial.SerialComManager;
/**
*
* #author Alexandros
*/
public class FindSerialPorts {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
String[] ports = scm.listAvailableComPorts();
for(String port: ports){
System.out.println(port);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
There is an error in code in statement
import com.embeddedunveiled.serial.SerialComManager;
Also it says on site second link
that The folder prebuilt contains ready-to-use jar file (scm-1.0.3.jar) that can be imported in any project and referenced right away.
Where do I find the prebuild folder?
thanks
----------Second Post----------------------------------------------
Moving on I have following code:
package serialportftdi;
import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;
/**
*
* #author Alexandros
*/
public class SerialPortFTDI {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
long handle = scm.openComPort("/dev/ttyUSB1", true, true, false); scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
scm.writeString(handle, "testing hello", 0) == true);
String data = scm.readString(handle);
System.out.println("data read is :" + data);
scm.closeComPort(handle);
}catch (Exception e) {
e.printStackTrace();
}
}
}
An error occurs at line : scm.writeString(handle, "testing hello", 0) == true);
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: not a statement
scm.writeString(handle, "testing hello", 0) == true);
^
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\src\serialportftdi\SerialPortFTDI.java:31: error: ';' expected
scm.writeString(handle, "testing hello", 0) == true);
^
2 errors
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\Alexandros\Documents\NetBeansProjects\SerialPortFTDI\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 3 seconds)
Also I am trying to understand the code but I don't understand what 'handle' does. Is this a handle as in C++? If yes then what exactly does? I was trying to find info about it in Java Deitel and wrox books but no info.
Ok! I played a little bit with code and now it works fine. Bare in mind that the ftdi cable must be inserted to the usb port that is found as being connected to COM4 serial port.
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package serialportftdi;
import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;
/**
*
* #author Alexandros
*/
public class SerialPortFTDI {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
SerialComManager scm = new SerialComManager();
long handle = scm.openComPort("COM4", true, true, true);
scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
scm.writeString(handle, "testing hello", 0); //== true);
String data = scm.readString(handle);
System.out.println("data read is :" + data);
scm.closeComPort(handle);
}catch (Exception e) {
e.printStackTrace();
}
}
}
I am developing a project that needs booth a web and desktop application. The web app receives the tasks from my clients and stores them (in database). The desktop application gets the tasks (from database) and execute them one by one. In my web application i am using java servlets, web services ...
Sometimes my glassfish server (v 3.1.2) freezes or he becomes blocked and needs to be restarted so he can continue work properly. I can detect this kind of error by monitoring him and find out when he freezes (by calling simple web service method that throws exception, simple http request that also throws exception etc).
I want my desktop application get the Glassfish server status and if
"Everything is ok" then "Do nothing"
"Server is down" then "Start Glassfish Server"
"I detect an error" then "Restart Glassfish Server"
"Application quit" then "Shutdown Glassfish Server"
Does anyone had this problem and has any solution. I am tired of manually restarting the glassfish server.
I run Glassfish 3.1.2 in production for months at a time without issue. I would suspect the freezing your are seeing is a problem with the application you have deployed to it.
I think you would be best served spending time investigating and remediating your hanging issue. Have you tried taking a thread dump of the Glassfish java process when this happens?
I found my own solution that i want to share.
When i detect that something is wrong with my Glassfish server, i restart it. This solutions only works on Linux (i will edit this answer if i find simular for windows users). Also u might have to add this line for your user in "/etc/sudoers" under root user, adrian is my username.
adrian ALL=(ALL:ALL) ALL
GlassFish Class: (U will need to change glassfishPath and domainName with yours)
package es.web.glassfish;
import es.os.linux.Konsole;
import java.io.IOException;
/**
*
* #author adrian
*/
public class Glassfish {
private final static String glassfishPath = "/home/adrian/glassfish-4.0/";
private final static String domainName = "domain1";
public static String startGlassfishServer() throws IOException, InterruptedException {
String command = glassfishPath + "bin/asadmin start-domain "+domainName;
return Konsole.executeCommand(command);
}
public static String stopGlassfishServer() throws IOException, InterruptedException {
String command = glassfishPath + "bin/asadmin stop-domain "+domainName;
return Konsole.executeCommand(command);
}
public static String restrartGlassfishServer() throws IOException, InterruptedException {
String command = glassfishPath + "bin/asadmin restart-domain "+domainName;
return Konsole.executeCommand(command);
}
}
Konsole Class:
package es.os.linux;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* #author adrian
*/
public class Konsole {
static Process process;
static BufferedReader reader;
public static String executeCommand(String command) throws IOException, InterruptedException {
String rez = "";
process = Runtime.getRuntime().exec(command);
process.waitFor();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
rez += line + "#";
}
return rez;
}
}
Test Class:
public class test {
/**
* #param args the command line arguments
*/
public static void main(String[] args){
try {
System.out.println("START");
System.out.println(Glassfish.startGlassfishServer());
System.out.println("RESTART");
System.out.println(Glassfish.restrartGlassfishServer());
System.out.println("STOP");
System.out.println(Glassfish.stopGlassfishServer());
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Test class output:
START
Waiting for domain1 to start ............#Successfully started the domain : domain1#domain Location: /home/adrian/glassfish-4.0/glassfish/domains/domain1#Log File: /home/adrian/glassfish-4.0/glassfish/domains/domain1/logs/server.log#Admin Port: 4848#Command start-domain executed successfully.#
RESTART
Successfully restarted the domain#Command restart-domain executed successfully.#
STOP
Waiting for the domain to stop #Command stop-domain executed successfully.#
When I run the code below I get the following error.
C:\Documents and Settings\BOS\Desktop\test>java -jar test.jar
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I've got these files in \test directory = crimson.jar robosuite-api.jar and test.jar.
Here is the example they give to launch a robot?
import com.kapowtech.robosuite.api.java.rql.*;
public class SimpleRunRobot {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: RunRobot <robotURL>");
System.exit(1);
}
try {
// Run the robot
RQLResult result =
RobotExecutor.getRobotExecutor().execute(args[0]);
// Output the results
System.out.println(result);
}
catch (RQLException e) {
System.out.println("An error occurred: " + e);
}
}
}
Why is this giving me that Unknown Source error?
package robosuite.robots;
import com.kapowtech.robosuite.api.java.rql.RQLException;
import com.kapowtech.robosuite.api.java.rql.RQLResult;
import com.kapowtech.robosuite.api.java.rql.RobotExecutor;
import com.kapowtech.robosuite.api.java.rql.construct.RQLObjects;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* <p>
* This is an autogenerated class. It has been generated from the
* <code>library:/test.robot</code> file.
*
* #author RoboSuite
*/
public class Test {
// ----------------------------------------------------------------------
// Class fields
// ----------------------------------------------------------------------
private static final String ROBOT_URL = "library:/test.robot";
private static final RobotExecutor ROBOT_EXECUTOR = RobotExecutor.getRobotExecutor(SingletonRQLEngine.getInstance());
private static final Converter CONVERTER = Converter.getInstance();
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
/**
* Creates a new Test instance that can be used to execute the
* <code>library:/test.robot</code>.
*/
public Test() {
}
// ----------------------------------------------------------------------
// Instance methods
// ----------------------------------------------------------------------
/**
* Executes this robot.
*
* #param test an input object to the robot.
* #return an array of output objects.
* #throws java.io.IOException if the execution fails for some reason.
*/
public Testst[] run(Test0 test) throws java.io.IOException {
try {
// Prepare input objects
List parameters = new ArrayList();
parameters.add(test);
RQLObjects inputObjects = CONVERTER.convertBeansToRQLObjects(parameters);
// Run robot
RQLResult rqlResult = ROBOT_EXECUTOR.execute(ROBOT_URL, inputObjects);
// Extract output objects
RQLObjects outputObjects = rqlResult.getOutputObjects();
List result = CONVERTER.convertRQLObjectsToBeans(outputObjects);
return (Testst[]) result.toArray(new Testst[result.size()]);
} catch (RQLException e) {
throw new IOException(e.toString());
}
}
/* ------------------------------------------------------------------- */
}
If your using Java 7, Read this.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922
Try
java -cp test.jar
include your other .jar files also
If you are using a manifest file make sure you have defined your main class.
for e.g.
Main-Class: test.MyApp
You have to add the name of the class having main() method in META-INF/manifest file.
Here is the link with more information :
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
Thanks.
Why is this giving me that Unknown Source error?
The "unknown source" messages are not an error. It is the JVM telling you that the code that you are executing was compiled without any debug information; e.g. with the -gLnone option. As a result, the source file names and line numbers that would normally be included in the stacktrace are not available.
In this case, the code is some platform specific stuff that is internal to the JVM. Don't worry about it ...
Just playing around with java trying to learn it etc.
Here is my code so far, using HtmlUnit.
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
/**
* #author
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("starting ");
Spider spider = new Spider();
spider.Test();
}
}
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
/**
* #author
*/
public class Spider {
public void Test() throws Exception
{
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");
System.out.println(page.getTitleText());
}
}
I am using Netbeans.
I can't seem to figure out what the problem is, why doesn't it compile?
The error:
C:\Users\mrblah\.netbeans\6.8\var\cache\executor-snippets\run.xml:45:
Cancelled by user.
BUILD FAILED (total time: 0 seconds)
The row in the xml is:
<translate-classpath classpath="${classpath}" targetProperty="classpath-translated" />
Test is declared to throw Exception. If you add "throws Exception" to your main method it should compile. For example:
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
System.out.println("starting ");
Spider spider = new Spider();
spider.Test();
}
What Steve said is correct. But maybe there are some problems with the uppercase character of Test. A method always starts with a lower case character. So test would be better.
Unchecking the "Compile on Save" option of the "Properties" tab in Netbeans 7.1.2 resolved a similar error message for me.