i execute java class to screenshot of my screen with following code:
import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FullScreenCapture extends JFrame {
private static final long serialVersionUID = 1L;
public static String capture() {
FullScreenCapture f = new FullScreenCapture();
String Ret;
try {
Thread.sleep(5000);
System.setProperty("java.awt.headless", "true");
Robot robot = new Robot();
String fileName = "D://FullScreenshot.jpg";
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize());
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println("Headless mode: " + ge.isHeadless());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, "jpg", new File(fileName));
Ret ="Capture Saved Successfully";
} catch (Exception e) {
System.out.println("Exception occurred");
Ret ="Wrong Error";
}
return Ret;
}
}
the program don't have any problems when executed in netbeans or in cmd,
but when load java of java class into oracle database
to call it as function,return error message java.awt.HeadlessException
You are using java.awt.Robot which needs a graphical, non-headless environment to work. As per Robot() javadoc:
AWTException - if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true
The Oracle database server doesn't provide a graphical environment so it can't run your code. As per User Interfaces on the Server Oracle docs:
Oracle Database furnishes all core Java class libraries on the server, including those associated with presentation of the user interfaces. However, it is inappropriate for code running on the server to attempt to materialize or display a user interface on the server. Users running applications in Oracle JVM environment should not be expected nor allowed to interact with or depend on the display and input hardware of the server where Oracle Database is running.
Related
I followed the steps on Google Cloud's Java and OpenTelemetry site (https://cloud.google.com/trace/docs/setup/java-ot) and made a simple hello world Java application locally and am trying to get my traces to show up on Google Cloud Trace using their trace exporter.
All the setup code is the same, and the program compiles and runs successfully. However, I don't see anything on the Trace dashboard. I know it is not an issue with IAM or my service account key because I ran the Python example and it shows up in Cloud Trace dashboard just fine.
Anyone have any guidance on why the Java version could be silently failing?
Thanks
package hello;
import org.joda.time.LocalTime;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
import java.io.IOException;
import java.util.Random;
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
import java.util.Collections;
import static java.util.Collections.singleton;
import java.time.Duration;
public class HelloWorld {
private static final Random random = new Random();
private static OpenTelemetry setupTraceExporter() {
try {
TraceExporter traceExporter = TraceExporter.createWithConfiguration(
TraceConfiguration.builder().setProjectId("my-test-id").build());
// Register the TraceExporter with OpenTelemetry
return OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())
.build())
.buildAndRegisterGlobal();
} catch (IOException e) {
System.out.println("Uncaught Exception");
System.out.println(e);
return null;
}
}
public static void main(String[] args) {
System.out.println("Starting the example application");
/* SET UP */
OpenTelemetry otel = setupTraceExporter();
/* Creating tracer */
Tracer tracer =
otel.getTracer("java foo");
Span span = tracer.spanBuilder("my span").startSpan();
// put the span into the current Context
try (Scope scope = span.makeCurrent()) {
System.out.println("Hello");
Thread.sleep(4000);
} catch (Throwable t) {
span.setStatus(StatusCode.ERROR, "error");
System.out.println(t);
} finally {
span.end();
}
System.out.println("Closing");
//otel.getSdkTracerProvider().shutdown();
}
}
After some debugging, I figured out the answer.
Seems like with this simple example, the BatchSpanProcessor is not a good idea because there is only one span that is getting traced.
SimpleSpanProcessor directly forwards the spans to Cloud Trace no matter what whereas BatchSpanProcessor waits until there is enough data before pushing to Cloud Trace. Hence why I was not seeing anything in Cloud Trace because BatchSpanProcessor hadn't registered enough spans for it to actually upload it to Google Cloud.
Span Processors Documentation
Change the following lines
return OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(traceExporter))
.build())
.buildAndRegisterGlobal();
Hope this helps others!
I want to load a URL in the user's default browser. And once the webpage gets loaded completely I want to take its screenshot.
Currently I am doing it like this:
import java.awt.Desktop;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import javax.imageio.ImageIO;
public class Example {
public static void main(String x[]) throws Exception
{
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://192.168.1.125:8001/html/en/default/process/ProcessDesigner.jsp?wftId=81454277&wftVersion=1");
desktop.browse(oURL);
desktop.wait();
//desktop.print("");
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
try {
Thread.sleep(20000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "jpg", new File("E:\\Akram\\SSFolder\\fi2.jpg"));
System.out.println("Done");
}
}
Here I am using the sleep function so that I should be able to capture snapshot after screen gets loaded, but some web pages may take longer to load.
So I want confirmation that the web page is ready / loaded completely so that I can take its snapshot.
How can I achieve this?
I'm stuck at work for like a week now,
can some1 with CM JavaAPI exprience guide me what am I doing wrong?
I try to connect to the server where the Case Manger is installed and start a
session, maybe I'm doing it all wrong but IBM Knowledge Center did not help.
GOT IT!
package *packacge*;
// jars from the the CM different installation folders on the server
import java.util.List;
import java.util.Locale;
import javax.security.auth.Subject;
import com.filenet.api.core.Connection;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.util.UserContext;
import com.ibm.casemgmt.api.CaseType;
import com.ibm.casemgmt.api.DeployedSolution;
import com.ibm.casemgmt.api.context.CaseMgmtContext;
import com.ibm.casemgmt.api.context.P8ConnectionCache;
import com.ibm.casemgmt.api.context.SimpleP8ConnectionCache;
import com.ibm.casemgmt.api.context.SimpleVWSessionCache;
import com.ibm.casemgmt.api.objectref.ObjectStoreReference;
public class CaseMgmtSession {
public static void main(String[] args) {
P8ConnectionCache connCache = new SimpleP8ConnectionCache();
Connection conn = connCache.getP8Connection("http://*ip of the server CM is installed on*/wsi/FNCEWS40MTOM/");
Subject subject = UserContext.createSubject(conn, *user of CM builder admin*, *pass of CM builder admin*, "FileNetP8WSI");
UserContext uc = UserContext.get();
uc.pushSubject(subject);
Locale origLocale = uc.getLocale();
uc.setLocale(Locale.ENGLISH);
CaseMgmtContext origCmctx = CaseMgmtContext.set(new CaseMgmtContext(new SimpleVWSessionCache(), connCache));
try {
// Code that calls the Case Java API or
// directly calls the CE Java API
// checking the connection is working
ObjectStore os = P8Connector.getObjectStore(*some object store name*);
ObjectStoreReference osRef = new ObjectStoreReference(os);
DeployedSolution someSolution = DeployedSolution.fetchInstance(osRef, *some deployed solution name*);
System.out.println(someSolution.getSolutionName());
List<CaseType> caseTypes = someSolution.getCaseTypes();
for(CaseType ct : caseTypes) {
System.out.println(ct.getName());
}
}
finally {
CaseMgmtContext.set(origCmctx);
uc.setLocale(origLocale);
uc.popSubject();
}
}
}
where P8Connector is a class i wrote that returns an objectstore
I dont know which version of Case Manager you are talking about. However, for 5.2.1.x, you will find ample references on IBM's site. For example - here and here.
i am running a virtual machine with VMware 9.0. I added the printers via the Settings tab in the VM. To see that my printers are availabe on my vm i wrote a little program:
import java.io.PrintStream;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
public class ShowPrinter {
public static void main(String[] args) {
PrintService lookupDefaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
if (lookupDefaultPrintService != null)
System.out.println("default: " + lookupDefaultPrintService.getName());
else {
System.out.println("default: null");
}
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService service : services)
if (service != null)
System.out.println("- " + service.getName());
else
System.out.println("- null");
}
}
This works well and i get some printers listed (including the one i want to use). I wrote a little program which should print something:
package virtualMachinePrinter;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
public class MyPrinter {
public static void main(String[] args) throws IOException {
File file = new File("C:/temp/printtest.txt");
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(inputStream, flavor, null);
try {
job.print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
inputStream.close();
System.out.println("Printing done...");
}
}
On my local machine this works just well, and if i change the default printer, it prints to it. On the virtual machine this works not as intended. The XPS document writer doesn't even start. If i try the same with pdf-printer, the page setup opens at least (but nothing is printed). If i start the little programm above inside a web-application on a Tomcat 7, it doesn't print anything. Independent of which default printer is used. In both cases the print order is added to the printing queue. But only outside of a Tomcat something is printed. Inside the Tomcat nothing is printed. I am using no security manager in my Tomcat.
Two actions solved this problem:
1. I had to use 32-bit version of Java. This fixed the problem printing with XPS document writer.
2. I had to update my printer drivers. This fixed the problem printing with the printer.
I tried this java code, in this code i move a file from from one directory to another one,
then execute the file. I am using Windows 7 OS.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.commons.io.FileUtils;
import org.omg.CORBA.Environment;
public class JFileChooserTest {
public static void main(String[] args) {
String filelocation="C:\\Users\\FSSD\\Desktop\\OutPut\\Target\\setup.exe";
File trgDir = new File(filelocation);
System.err
.println("file location>>>>>>>>>>>>>>>>>>>"
+ filelocation);
File desDir = new File(
"C:\\Users\\FSSD\\IndigoWorkSpace\\Swing\\test");
try {
FileUtils.copyFileToDirectory(trgDir, desDir);
// FileUtils.copyDirectory(srcDir, trgDir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Runtime rt=Runtime.getRuntime();
try {
Process p=rt.exec("runas /user:FSSD test/setup.exe");
//Process p= rt.exec("test/setup.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
While i execute this i got "740: The requested operation requires elevation " error , if any possibilities to resolve it.
Short answer is that you can't do it in-process. You need to launch a new process that is elevated at the command line. (see the elevate command).
Please see this question and answer here - they address your issue.
Service host local system network restricted 10
HKEY_LOCAL_MACHINE > SYSTEM > ControlSet001
registry value named Start in the right hand panel and double click on it.
Link