I am making a program for computer surveillance at the moment.
It's for a competition in my country Croatia(InfoKup).
I have several options for sending command to another PC, but I
want to make the possibility for the command extension for people
who know Java. So I want to make the user be able to add some of his
custom commands for the program. For example something like
Minecraft mods. I know it is possible, but how would I go about
doing that.
Any help would be greatly appreciated. My code on GitHub:GitHub
Don't mind the stream thing.
It's something my friend is experimenting with.
EDIT: e.g.
Currently I have the possibility to send popups to another PC. What if the extension maker knew the code to send cmd commands and wants to add that function. He makes an extension and puts it in the extension folder. Voila we have a new possibility.
EDIT 2:
Don't be so harsh on me pls :). Thx for the dynamic class loading tip. I have been looking into that, and it looks promising.
Basically what I want to have possible is the user to drop the "mod/extension/whatever" in the "mod/extension/whatever" folder, and the program would load it and put all of the buttons declared in the class in to the GUI, and with them the function. I think I'm getting the hang of this, but any tips would be helpful.
e.g.
package sth.sth;
import blah.blah.*;
public class ClassSTH extends SchoolarButton{
public ClassSTH(String params){
super(params);
}
#Override
public void OnClick(){
doStuff();
}
}
EDIT:
The problem is easily solvable using Java Reflection! I wish someone posted that as an answer befpre blatanty downvoting a question because pf a GitHub link that was there to prpve that I've actually done something.
This kind of thing can be accomplished by using Java Reflection!
How to load and invoke a method on an external jar:
File f = new File("plugin.jar");
URLClassLoader cl = new URLClassLoader(new URL[]{f.toURI().toURL(), null});
Class<?> clazz = cl.loadClass("epicurus.Client");
Method main = clazz.getMethod("main", String[].class);
main.invoke(null, new Object[]{new String[]{}});
Related
so i tried to make a simple PlayerJoinEvent (AKA. PlayerFirstJoinEvent) for my server. Is there a way to do that? I want to run my code when player joins the server first time. I have tried multiple options like using if(player.hasPlayedBefore()) but it doesent want to work! So, do you have an idea how to fix it or do it with a different method? Thanks to everyone for help!
You haven't probably deleted player data! If new player joins, new player data are created, only then does Spigot know, that the player is new to the server!
To do so:
Shutdown your server
Go to server-folder/world/playerdata/*player-uuid*.dat
Delete player-uuid.dat
Start the server
Warning: This deletes player inventory!
If your server is in offline mode, and you don`t know what your UUID is, use this online tool.
If your server is in online mode, and you don't know your UUID, use namemc
Code to test the event:
#EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if(!event.getPlayer().hasPlayedBefore()) {
event.getPlayer().sendMessage("Hi!");
}
}
Don't forget to register this event in your main class!
So i solved it and Thanks to everyone for help there wasnt any problem with hasPlayedBefore().
I am working on a GUI application that uses JavaFX(not fxml) and exported as a JAR. For slow machine, impatient user click more than once on JAR, and multiple instances of application started.
I'm looking for a solution to let only one instance can be run at a time on a system and if the user clicks again while the application is running nothing happens. I think it's called singleton but don't know how to implement it.
You could try JUnique. It's an open source library doing exactly what you ask for. Import junique-1.0.4.jar to your project as a library. It's just 10kb file.
It's manual neatly describes how to implement it on a project. For a JavaFX application, implementation would look something like this:
Make sure to import these classes to your main
import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;
public static void main(String[] args) {
String appId = "myapplicationid";
boolean alreadyRunning;
try {
JUnique.acquireLock(appId);
alreadyRunning = false;
} catch (AlreadyLockedException e) {
alreadyRunning = true;
}
if (!alreadyRunning) {
launch(args); // <-- This the your default JavaFX start sequence
}else{ //This else is optional. Just to free up memory if you're calling the program from a terminal.
System.exit(1);
}
}
One easy solution that I've used is, when you start the application, it creates a file (I named it .lock but you can call it whatever you want), unless the file already exists, in which case the application terminates its execution instead of creating the file.
You will need to bind your application with a resource. It can be a file, port etc.
You can change the code on startup to check if the file is locked. The below code will give you some idea
FileOutputStream foStream = new FileOutputStream("/tmp/testfile.txt");
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock();
If you'd properly package your JavaFX code as a real application instead of just throwing it into a jar, you might get that functionality for free and without all these hacks. If I package my JavaFX code on my Mac with the jpackage tool, the result will be a full featured macOS application. That means that when I double-click its icon somewhere several times, only one instance of the application will be started. This is the default behaviour on Macs and properly packaged JavaFX applications just stick to that rule too. I can't say however what the behaviour on Windows or Linux is because I currently don't have such a box running. Maybe someone who knows can add this as a comment.
I've been working in a little project to get a bit of practice programming. It's basically done, but I won't be satisfied until I can use images properly, a bit of help would be appreciated.
So, currently I'm using the the getImage method from the ImageIcon class, like so:
Image body = new ImageIcon("C:/Users/Centollo/Documents/NetBeansProjects/Chess/build/classes/chess/img/WhiteBishop.png").getImage();
I've been trying to figure out how to do the same thing without using an absolute path, but I don't know how to make the images a part of the jar so that it works fine in any other machine.
All I need to know is where to put the images and how would the code to access them look like.
Try to explain it like I'm stupid, please. I've read answers to similar questions but I can't make heads or tails of them.
I'm working in NetBeans with a "chess" package with all the .java and a "chess.img" package with all the .png.
If your class extends from JFrame, you can do this:
Image image = new ImageIcon(this.getClass().getResource("/images/MyImage.jpg")).getImage();
If your class extends Applet, you can go this way:
private URL base = null;
private Image myImage = null;
try
{
base = getDocumentBase();
} catch (Exception e)
{
e.printStackTrace();
}
myImage = getImage(base, "images/MyImage.jpg");
A very quick google search yields this:
URL resource = getClass().getClassLoader().getResource( "img/WhiteBishop.png" );
Image body = new ImageIcon( resource );
There are a couple ways to do this but here's the way I would suggest:
Make sure the chess.img folder is in your application's classpath. Then try referring to the path like chess.img/image (yes, you can use forward slash in windows.)
If that doesn't work use:
ChessClass.class.getClassLoader().loadResourceAsStream("/chess.img/image");
Note the forward slash at the beginning of the file reference. This points to the root of the classpath. It's a bit confusing as someone with unix/linux experience might think it refers to the root of the file system. This tends to work better than the other answer given for reasons I knew 10 years ago. This is an ugly bit of Java that was never quite cleaned up.
I have integrated the GWT application with Chrome packaged app with help of DirectLinkerinstaller like the code below:
public class CSPCompatibleLinker extends DirectInstallLinker {
#Override
protected String getJsInstallLocation(LinkerContext context) {
return "com/google/gwt/core/ext/linker/impl/installLocationMainWindow.js";
}
}
But now I want to call print function from Chrome packaged app. When I call window.print() it allows me to print current window, but I need to open a new separate window and print that.
Could you anyone please help me in this?
I can't answer anything about GWT or DirectLinkerinstaller, but here's an answer about Chrome Apps, assuming that's what you're asking about:
You use the chrome.app.window.create API to create a window. Then, you can call the print method for that window.
In my apps, I seldom want to print what's in a window, but rather something I've generated specifically for printing. For that, I create a PDF with jsPDF (Google it), which works well. Then I display the PDF in a window, and let the user print the PDF (or save it).
I am trying to create a console that would work as a shell for a custom programming language. It would be very similar to the pydev interactive console.
Currently, my RCP uses the basic TextConsole and is connected to the shell via pipes so it just displays whatever the shell displays and if the user enters anything in the RCP console, the same is written in the shell.
I want to be able to do a bit more such as move the caret position, add events for up and down arrow keys etc. I believe to do that I need to add a StyledText widget to the console which is done via the ConsoleViewer.
So my question is, that is there any way for me to either override the TextConsole's ConsoleViewer or if I were to extend TextConsole and create my own, then how do I link it with the launch configuration (the one that connects the shell via pipes)?
Also, to get the current default console I use DebugUITools.getConsole(process).
I'm sorry if I haven't put all the information needed; it is a bit difficult to explain. I am happy to add more information.
An idea...
From what I understand I can create a TextConsolePage from the TextConsole using createPage(ConsoleView). Once I have the page I can set the viewer via setViewer(viewer). Here I thought if I create my own viewer (which will have the appropriate stylewidget) then that could be a lead. The only problem is that the viewer needs a Composite and I can't seem to figure out where to get that from.
Why don't you just follow what PyDev does (if you're able to cope with the EPL license)?
The relevant code may be found at:
https://github.com/aptana/Pydev/tree/ad4fd3512c899b73264e4ee981be0c4b69ed5b27/plugins/org.python.pydev/src_dltk_console
https://github.com/aptana/Pydev/tree/ad4fd3512c899b73264e4ee981be0c4b69ed5b27/plugins/org.python.pydev.debug/src_console
So I thought I would answer this myself as I was finally able to accomplish the console. It still is a working prototype but I guess as you keep adding things, you can clean up the code more and more. For my current purposes this is how it worked.
If you want the short version, then I basically mimicked the ProcessConsole provided by Eclipse as that is what I needed: a console in which I can connect a process but since the ProcessConsole is internal, I like to avoid extending those classes.
Following is an outline of the classes I used to achieve interaction with my console. I am not going to give the pretext as to where MyConsole was created. Basically, instead of using DebugUITools.getConsole(myProcess), I used my own myProcess.getConsole() method. MyProcess extends RuntimeProcess.
class MyConsole extends IOConsole {
private IOConsoleInputStream fInput;
private IOConsoleOutputStream fOutput;
private IStreamsProxy fStreamsProxy;
private ConsoleHistory history;
//This is to remember the caret position after the prompt
private int caretAtPrompt;
/* in the console so when you need to replace the command on up and down
* arrow keys you have the position.
* I just did a caretAtPrompt += String.Length wherever string was
* appended to the console. Mainly in the streamlistener and
* InputJob unless you specifically output something to the output
* stream.
*/
//In the constructor you assign all the above fields. Below are some
//to point out.
//fInput = getInputStream();
// fStreamsProxy = process.getStreamsProxy();
// fOutput = newOutputStream();
//We must override the following method to get access to the caret
#Override
public IPageBookViewPage createPage(IConsoleView view) {
return new MyConsolePage(this, view);
}
//After this I followed the ProcessConsole and added the
//InputJob and StreamListener
//defined in there.
}
class MyConsolePage extends TextConsolePage {
//Not much in this class, just override the createViewer
// to return MyConsoleViewer
}
class MyConsoleViewer extends TextConsoleViewer {
//This is the most important class and most of the work is done here
//Again I basically copied everything from IOConsoleViewer and then
//updated whatever I needed
//I added a VerifyKeyListener for the up and down arrow
//keys for the console history
MyConsoleViewer (Composite parent, MyConsole console) {
//I have omitted a lot of code as it was too much to put up,
//just highlighted a few
getTextWidget().addVerifyKeyListener(new MyKeyChecker());
}
class MyKeyChecker implements VerifyKeyListener {...}
}
This is the code for ProcessConsole.
This is the code for IOConsoleViewer.
The ConsoleHistory class I created just had a doubly linked string list to save all the commands the user entered. Quite a simple class to create.
Once you look at the Eclipse classes (ProcessConsole and IOConsoleViewer) it is actually all quite self explanatory. I haven't put in much code here because there is quite a bit. But hopefully this gives some direction as I was completely lost when I started.
I am happy to answer questions though and add more specific code if anyone asks.