How to take a screenshot with JNA in Windows? - java

I found a few code examples, but I don't know with which JNA versions I can use which methods. I did only find snippets, where classes were missing and I wasn't able to import them.
I would like to know which JNA version I should use and how to get a screenshot as BufferedImage.
A list of required imports would also be great.

It looks like there are several examples at this link. I'll discuss one below (#3) for discussion purposes, but you may find one of the other examples more applicable to your situation and hopefully this answer will help you understand the process.
Before the example, I will answer your question "which JNA versions"... you should use the latest version in almost all cases. JNA is a user-supported library, and the core JNA code doesn't change much but each new version adds more user-contributed mappings to native functions. Note their FAQ question, "JNA is missing function XXX in its platform library mappings" and the answer, "No, it's not, it's just waiting for you to add it :)". If the mapping you need is not in JNA, you can simply add it using the example provided, for your immediate needs. Better yet, contribute your mapping to the JNA project so that the next person in your situation will benefit from the work you've done!
Now, example #3 from the link takes a screenshot of the entire screen and returns it as a BufferedImage object. The full source code for that example shows all the imports you will need, most from JNA's WinGDI class.
If you scroll to the bottom of the class you may also see that the authors have extended two JNA platform interface contributions with mappings that aren't in JNA (or weren't in 2010 when that code was written). You will have to do similar mappings (and perhaps contribute them to their respective JNA classes when you're done).

Related

Oleacc dll support in JNA

I am trying to use the JNA library for calling the Oleacc dll's AccessibleObjectFromWindow method for a Java project that I am working on.
I went through the JNA documentation as well as searched for an example online but could not find a good reference for using AccessibleObjectFromWindow in Oleacc dll with JNA.
Can someone having a good background on JNA library please confirm whether JNA includes Oleacc dll's functionality?
If not any alternatives to using Oleacc dll from a Java program is also much appreciated.
Thanks..!
When you ask the question "JNA includes" it's helpful to point out there are two portions of the JNA project. There's the jna artifact, which includes core functionality, and the jna-platform artifact which contains user-submitted mappings of JNA for various platforms. I highlight "user-submitted" as JNA is a community maintained project, and the FAQ answer to "JNA is missing function XXX in its platform library mappings" is "No, it's not, it's just waiting for you to add it :)".
The Oleacc dll is one of those instances. It's not yet in the jna-platform artifact, but it could be if a user submitted it. See, for example, a user in 2015 mapped this library themselves, but did not contribute it to the community, so here you are 5 years later reproducing that effort! If you scroll down in that JNA issue you'll see some sample code implementing your method and a few others, that it would be fantastic if you could contribute to JNA!
Meanwhile, when a mapping isn't in JNA, the above FAQ link gives a template for how to implement it. In your specific case, the code to implement AccessibleObjectFromWindow would be simple for you to do in your own project. Create an Oleacc class with these contents:
public interface Oleacc extends StdCallLibrary, WinUser, WinNT {
Oleacc INSTANCE = (Oleacc) Native.loadLibrary("oleacc", Oleacc.class, W32APIOptions.DEFAULT_OPTIONS);
HRESULT AccessibleObjectFromWindow(HWND win, int objID, Guid.REFIID iid, PointerByReference ptr);
}
And you're done! So is that mapping in JNA yet? No, but hopefully after you've implemented it and tested your code, you can contribute your mapping to the project so that the next person who needs to do so can find it in JNA!

Cannot use com.sun.net.httpserver - Java 8

I am trying to create a simple server program to handle http requests. So with minimum search, I stumbled upon the oracle documentation for the httpserver class, inside the com.sun.net package. I'm relatively new to Java, so I thought that a class "sponsored" by Oracle itself would be included in the default libraries.
Unfortunately, that was not the case. After a lot of trials for possible syntax-import errors (various kinds of error arouse) and having installed the Oracle JDK 8 correctly on my Ubuntu machine, the compiler said that the package did not exist. What do I have to do to get the package to work?
I did finally make it work. Mostly, it was a misunderstanding from my place, since I only imported up to a point that was wrong - that is,I only imported com.sun.net.httpserver, thinking the latter part was the actual class I wanted, but it was merely the package name. So then I proceeded to import com.sun.net.httpserver.HttpServer, then the rest of my classes. Finally a com.sun.net.httpserver.* would work perfectly fine. It seems stupid now that I figured it out, but I think I will leave it here just in case anyone has the same misunderstanding - I already see 1 favourite on the question. And of course, as others have pointed out, the package is not part of the standard java libraries, but I used Oracle Java specifically for that.
P.S. The class is really useful, unlike what the other answer implies, but now I have stumbled upon another problem regarding reading the request body right, something that might have to do with the locale of the client-server, and I will now procced to search that.. Just a warning for anyone thinking of using the package.
Firstly try to avoid com.sun.xxx package, as those are internalls of Oracle/Sun java implementation.
Secondly, why not use libraries from apache ? See https://hc.apache.org/
EDITED:
You can also look on http://sparkjava.com/ , not tested but examples looks promising and they are using Java 8 nice features.

Java: How can I create good looking class docs with Javadoc?

I'd like to create HTML documentation for a Java class library that includes
programming guide information - beyond just a class reference
code examples in the reference doc
collapsible regions
I want it to look well-styled. Something like this:
I think that Javadoc exposes a doclet API that allows other parties to provide doc generation integrated in Javadoc. Is there a doclet option that I can use (for free) that does something a little nicer than the standard javadoc output?
I tried googling for this but no luck.
Suggestions?
Addendum
I found doclet.com but I don't know how to evaluate the options. Recommendations?
In particular, XHtmlDoclet sounds like it wants to address these issues. Anyone used it? I can't get it to work. Also the example offered by the XHTML Doclet guys doesn't look any better than the docs generated from c.1998 javadoc .
Related:
Why does Javadoc generate non-compliant XHTML?
This question got me thinking (ok, Googling) and I stumbled on this set of taglets. Never tried it, but it looks pretty promising.
Perhaps you're thinking of Java doclet API:
http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html
It appears that you're adding examples and some text. I think it's entirely possible with existing javadocs. The problem will be the content rather than the rendering.
Castor used doclet to ease its config burden. So did EJB 1.0. Take a look at those if you'd like.

Structural comparison of two ASTs in Eclipse

I am working on an incremental builder for Java code in Eclipse. Eclipse provides a ResourceDelta that tells me which resources have changed since the last build. However, I would like to have more detailed information, e.g. what methods or what field definitions changed. There seems to be functionality similar to what I want in the "compare with -> each other" view. However, this code is quite disconnected from the build engine and seems incompatible with ResourceDeltas. What would be a good way to figure out what I want? The best solution I can see is to compare two ASTs, but I also could not find any built-in support for that.
JavaCore does supply this information via the IElementChangedListener and IJavaElementDelta interfaces. Here's a quick code sample to get you started:
JavaCore.addElementChangedListener(new MyJavaElementChangeReporter(), ElementChangedEvent.POST_RECONCILE);
More details available in Manipulating Java code from the JDT Plug-in Developer Guide.

Programmatically inspect .class files

I'm working on a project where we're doing a lot of remote object transfer between a Java service and clients written in other various languages. Given our current constraints I've decided to see what it would take to generate code based on an existing Java class. Basically I need to take a .class file (or a collection of them) parse the bytecode to determine all of the data members and perhaps getters/setters and then write something that can output code in a different language to create a class with the same structure.
I'm not looking for standard decompilers such as JAD. I need to be able to take a .class file and create an object model of its data members and methods. Is this possible at all?
I've used BCEL and find it really quite awkward. ASM is much better. It very extensively uses visitors (which can be a little confusing) and does not create an object model. Not creating an object model turns out to be a bonus, as any model you do want to create is unlikely to look like a literal interpretation of all the data.
I have used BCEL in the past and it was pretty easy to use. It was a few years ago so there may be something better now.
Apache Jakarta BCEL
From your description, it sounds like simple reflection would suffice. You can discover all of the static structure of the class, as well as accessing the fields of a particular instance.
I would only move on to BCEL if you are trying to translate method instructions. (And if that's what you're trying to automate, good luck!)
I'm shocked that no one has mentioned ASM yet. It's the best bytecode library your money can buy. Well, ok it's free.
JAD is a java decompiler that doesn't allow programmatic access. It isn't readily available anymore, and probably won't work for newer projects with Java7 bytecodes.
I think javassist might help you too.
http://www.jboss.org/javassist/
I have never had the need of using it, but if you give it a try, would you let us know your comments about it?
Although I think it is more for bytecode manipulation than .class inspection.

Categories