This tutorial will tell you how to have your server start in the tray!
There are some options i have added to it aswell to customize it easily
Difficulty: 3/10
Files edited:
Server.java
Misc.java (Optional)
Run.bat (Optional)
Files created:
SysTray.java
Index:
Step 1: Making the tray
Step 2: Logging to file (Optional)
Step 3: Making the SysTray.java class
Step 4: Adding parameters to run.bat (Optional)
Step 1:
server.Server.java
Open Server.java and search for:
Code:
public static void main(String[] args) {
You should see:
Code:
public static void main(String[] args) {
String a1 = "43594";
try {
socketListener = new SocketListener(Integer.parseInt(a1));
} catch (Exception e) {
/*
* If this happens then the specified port is most likely already in
* use.
*/
e.printStackTrace();
return;
}
loadBannedUsers();
engine = new Engine();
playerSave = new PlayerSave();
socketListener.run();
}
Change this to:
Code:
public static void main(String[] args) {
SysTray.createTray();
if (args.length < 2) { // Change this number to 1 if not doing step 2!
Misc.println("Not enough parameters!");
return;
}
try {
socketListener = new SocketListener(Integer.parseInt(args[0]));
} catch (Exception e) {
Misc.println("Port in use!");
return;
}
try {
logName = args[1]; // Delete this line if not doing step 2!
loadBannedUsers();
engine = new Engine();
playerSave = new PlayerSave();
socketListener.run();
} catch (Exception e) {
Misc.println(e.toString());
for (StackTraceElement layer : e.getStackTrace()) {
Misc.println(layer.toString());
}
main(args);
return;
}
}
Now you need to add the logName at the top of the class:
Code:
public static String logName = "ServerLog.txt";
// Do not add if not doing step 2!
And the import:
Code:
import server.io.SysTray; // make sure you replace server!
Step 2:
server.util.Misc.java
NOTE: This step is optional! Only do this step if you wan your log to be saved to a file. Also follow the instructions in Step 1 (comments in code) if you are not doing this step!
Open Misc.java and search for:
Code:
public static void println(String message) {
You should see:
Code:
public static void println(String message) {
System.out.println(message);
}
Change this to:
Code:
static SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd kk:mm:ss");
public static void println(String message) {
String timestamp = "[" + sdf.format(Calendar.getInstance().getTime()) + "] ";
System.out.println(timestamp + message);
try {
BufferedWriter out = new BufferedWriter(new FileWriter("../../" + Server.logName, true));
out.write(timestamp + message + "\r\n");
out.close();
} catch (IOException e) {}
}
And the imports:
Code:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import server.Server; // make sure you replace server!
Step 3:
server.io.SysTray.java
Now you need to create the new class: SysTray.java
Open up the new class and paste in:
Code:
package server.io; // make sure you replace server!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.plaf.metal.MetalIconFactory;
import server.util.Misc; // make sure you replace server!
public class SysTray {
// This method can be changed to use your own icon if you choose
private static Image getImage() {
Icon defaultIcon = MetalIconFactory.getTreeComputerIcon();
Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
return img;
}
private static PopupMenu createPopupMenu() {
PopupMenu menu = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(exit);
return menu;
}
public static void createTray() {
TrayIcon icon = new TrayIcon(getImage(), "Servername here!", createPopupMenu()); // Change this to your server name!
try {
SystemTray.getSystemTray().add(icon);
} catch (AWTException e) {
Misc.println("Error creating tray.");
}
}
}
NOTE: Make sure you pay attention to the comments in the code! server should be replaced with your server's main class name.
Step 4:
Run.bat
NOTE: This step is optional! Only do this step if you have completed Step 2! Also follow the instructions in Step 1 (comments in code) if you are not doing this step!
Open up run.bat and look for a line that looks like this:
Code:
java yourserver/Server
Delete this line and change it to:
Code:
start javaw yourServer/Server yourPort yourLogName
yourServer = your server's main package name
yourPort = the port you are running your server on
yourLogName = the filename of your log (Eg. ServerLog.txt)
If you dont want to show your server's console at the start then change the last line (should be either exit or pause) to exit
If you want it to show up with a Press any key to continue... line change it to pause
If you want your normal console to show aswell as the tray icon change start javaw to just java
This works on the client too!
if you don't want to show your client's console change it's java line to start javaw!
Now your ready to try your server out!
Don't leech this tutorial!
If you have anything to add then please post!
Tutorial by: XThexder