Disabling the Keyboard and mouse - java

I am a newbie to JNA and I have this code which is supposed to block the input by calling the dll file in win7. But when I run this code, nothing happen. There is no compilation error and I can't figure out why it doesn't block my keyboard and mouse. Please guide me.
public class BlockInput {
public static void main(String[] args) {
NativeLibrary lib = NativeLibrary.getInstance("user32");
Function fun = lib.getFunction("BlockInput");
System.out.println("Lib :" + lib + ".\nFun " + fun + ".");
fun.invoke(new Object[]{Boolean.TRUE});
try {
Thread.sleep(10000);
} catch(InterruptedException ie) {}
lib.dispose();
}
}
EDIT : With Native.getLastError(); I came to know that whicle accessing the dll file I recieve the error 5 (Access denied).Is there any possible way to gain access,so that I can make it work?

If you are running on Windows Vista or Windows 7, you might need to run the program as administrator. Make a batch file that runs your Java class to make things easier.

Try This - A Native Global keyboard and mouse listeners for Java.
JNativeHook

Related

Not able us Virtual Threads using Project Loom

I was exploring the Virtual Threads in Project Loom. The Documents say it as straight forward with simple lines of code as below:
Thread.startVirtualThread(() -> {
System.out.println("Hello, Loom!");
});
Or
Thread t = Thread.builder().virtual().task(() -> { ... }).start();
I have tried both of them, For the first one, I receive an error
The method startVirtualThread(() -> {}) is undefined for the type Thread
And for the second one
- The method builder() is undefined for the type Thread
One browsing, found that lombok is required, Installed lombok as well. However it doesn't show up in Eclipse About section, I am able to use lombok, But still my issue is not resolved.
Below link show the documentation, I am referring to.
enter link description here
Sample Code:
public class threads {
public void simpleThread() {
Thread start = Thread.builder().virtual().task(() -> {
System.out.println("Hello World");
}).start();
Thread.startVirtualThread(() -> {
System.out.println("Hello, Loom!");
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
threads trd = new threads();
trd.simpleThread();
}
}
It looks like older versions of Eclipse is giving a compilation error when calling the new Thread methods related to Loom.
Please try to use the latest Eclipse (currently 2020-09) with an OpenJDK Project Loom Early-Access Build.
You can make sure that this is an Eclipse related issue by compiling and running your program directly from the command line (using javac and java commands).
For ubuntu system:
Set the java 16 path in .bashrc file. Make sure only have java 16 path present in the file. If any other java path is mentioned then following command may not work.
If you want to confirm if the java version is set to 16 then execute java -version.
Then you can try directly compile your loom class through following command.
javac className.java
java className
It worked for me.
Even when you get the compilation problems go away, this might or might not print anything.
A virtual thread needs a carrier (a native thread to be executed on); and if the native thread finishes earlier then the virtual one starts, there is no such carrier; thus you get no printing. There are a couple of ways to work around that.
The simplest (just to see that this works), is to make the carrier threads sleep for a short while:
Thread.startVirtualThread(() -> {
while (true) {
System.out.println("trying " + Thread.currentThread().getName());
}
});
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10));
On my machine this gives enough time for some output to be generated.
Another way would be join on the carrier:
Thread t = Thread.startVirtualThread(() -> {
System.out.println("trying " + Thread.currentThread().getName());
});
t.join();
This works for demo purposes, but in real life you probably need an executor. One way to supply it would be via :
Thread.builder()
.virtual(Executors.newFixedThreadPool(1))
.task(() -> {
System.out.println("started");
})
.build()
.start();
System.out.println("done");
You can even use Executors::newVirtualThreadExecutor where the current documentation states:
Creates an Executor that starts a new virtual thread for each task
So what you could do, is something like:
ExecutorService service = Executors.newVirtualThreadExecutor();
service.execute(() -> {
System.out.println(Thread.currentThread().getId());
});
service.execute(() -> {
System.out.println(Thread.currentThread().getId());
});
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10));

Externally executable application fails for some application?

I'm wanting to launch the program from a Java application, with some luck. Most programs are started without problems, but some seem to not execute properly(?).
The code I'm using is very simple:
private static void exec() {
ProcessBuilder builder = new ProcessBuilder("C:\\Users\\Fillipuster\\AppData\\Local\\Discord\\Update.exe");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
...and works for almost all executables (*.exe). Discord is purposefully placed in the example, as it is one of the programs that cause this problem. (along with Messenger For Windows and GOG Galaxy).
The behavior is simple, and the same for all executable that causes this; a command prompt quickly pops into existence and then promptly disappears (pun intended) - resulting in the application not being launched.
Sifting through Google and Stack Overflow proved a futile effort, and at this point, I'm at a complete loss.
Any help/input is much appreciated.
Thanks to John, who pointed out that even launching the the Update.exe file "manually" results in the same behavior, I've found the problem.
It seems that when launching Discord successfully, one is actually launching a shortcut that gives a parameter to the executable. In this case:
--processStart Discord.exe
This means that the following code will in fact start Discord:
private static void exec() {
ProcessBuilder builder = new ProcessBuilder("C:\\Users\\Fillipuster\\AppData\\Local\\Discord\\Update.exe", "--processStart", "Discord.exe");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks to John and all the other commenters.

Terminal loses focus on Mac OS X when calling java.awt.Color.*

I'm writing a command line program in java, and using java.awt.Color for utility purposes (there is no GUI involved). But when I reference java.awt.Color.RED (for instance) it causes the terminal to lose focus. This only happens on Mac, it works fine on Linux and Windows. I suspect that the static initializers are the problem, but I'm not really sure where to go from here, as this is not a problem that I can debug (since the problem isn't going to happen if the terminal doesn't have focus to begin with).
Here is a small code snippet that demonstrates the problem. (Build the jar, then run it from the terminal.)
public class Sandbox {
public static void main(String[] args) throws Exception {
System.out.println("Note that we have focus right now. Calling System.out.println(Color.RED) in 5 seconds.");
Thread.sleep(5000);
System.out.println(java.awt.Color.RED);
System.out.println("Note now that focus has been lost.");
Thread.sleep(5000);
System.out.println("Ending program.");
}
}

CGI in Java - Legacy but interesting (for educational purposes)

I folks:
I know that CGI is jurassic and before all of you call me lunatic, I must say that this question is only for EDUCATIONAL PURPOSES (in real cases I use JSP).
I'm trying to code a "Hello World" CGI in Java, and I'm unsucessful.
So I try the same job in C#.
Both programs (C# and Java) are totally equal (line by line). The C# works and the Java don't.
Here's the code in C#:
namespace CGI_CSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Content-Type: text/html\n"); // the extra "\n" is needed
Console.WriteLine("<html>");
Console.WriteLine("<head>");
Console.WriteLine("<title>CGI - C#</title>");
Console.WriteLine("</head>");
Console.WriteLine("<body>");
Console.WriteLine("<h1>Hello World !</h1>");
Console.WriteLine("</body>");
Console.WriteLine("<html>");
}
}
}
In the browser URL I wrote: http://localhost/CGI_CSharp.exe and BINGO! The C# code WORKS !
Now the same code in Java:
public class CGI_Java
{
public static void main(String[] args)
{
System.out.println ("Content-Type: text/html\n");
System.out.println ("<html>");
System.out.println ("<head>");
System.out.println ("<title>CGI - Java</title>");
System.out.println ("</head>");
System.out.println ("<body>");
System.out.println ("<h1>Hello World !</h1>");
System.out.println ("</body>");
System.out.println ("</html>");
}
}
Now I've tried the URL:
http://localhost/java.exe%20CGI_Java
(as you know, the %20 is the space => (http://localhost/java.exe CGI_Java)
I get:
HTTP 404.0 - Not Found
So, I try again, now with a batch file (CGI_Java.bat) with a single line of text:
java.exe CGI_Java (content of the CGI_Java.bat)
And now, I try the URL:
http://localhost&/CGI_Java.bat
Now the browser shows:
C:\inetpub\wwwroot>java.exe CGI_Java
This is the prompt followed by the command I wrote in the batch file.
Can someone help me?
Thanks in advance.
What server are you using?
Is java.exe on the path for the server?
(Note that if you alter the environment variable for the path you will have to restart the server for it to pick up the changes).

Having trouble starting a program with the Runtime.getRuntime().exec(command)

I'm currently writing a Java program that can open .exe programs on my PC, like MS Word for example.
I am having a problem though, because Runtime.getRuntime().exec() will only successfully open certain programs. I have used the exact same code for all the programs, but regardless, some programs won't open.
Here is my code for a program I downloaded, Picasa 3:
class picasaHandler implements ActionListener
{
public void actionPerformed(ActionEvent r)
{
try
{
Runtime.getRuntime().exec("cmd /c start Picasa3.exe");
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find Picasa 3");
}
}
}
So my question is, why won't Runtime.getRuntime().exec() run all the programs I use it on, and how do I run programs like Picasa 3, that I cannot run at this moment with this method.
I'm guessing that Picasa3.exe is not on your %PATH% anywhere so it doesn't know how to load it. Have you tried specifying the full path to Picasa3.exe?
Runtime.getRuntime().exec("cmd /c \"c:\\program files (x86)\\Google\\Picasa3\\Picasa3.exe\"")
File file=new File("picassa3");
String filename=file.getAbsolutePath(file);
try
{
Runtime.getRuntime().exec(filename);
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find the file");
}
Runtime's exec can only start applications that are on the Windows path. Some programs are automatically on the path, while others, like Picasa, is not.
The only work-around for this is to determine the correct path and then launch that application.
This might work for you.
If you want to run a certain program using Runtime.exec(), just add it's installation path to path variable in your System Variables. To find it's installation path, simply right click on it's shortcut and select "Find Target". Then concat that entire address at the end of your path Variable.

Categories