Thread: [508] Running a server in the tray!

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 [508] Running a server in the tray! 
    xthexder
    Guest
    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
    Reply With Quote  
     

  2. #2  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,146
    Thanks given
    256
    Thanks received
    1,252
    Rep Power
    3714
    Nice job but not gonna use



    Reply With Quote  
     

  3. #3  
    Registered Member

    Join Date
    Mar 2008
    Posts
    226
    Thanks given
    11
    Thanks received
    1
    Rep Power
    163
    Good job!
    Not going to use tho.
    Reply With Quote  
     

  4. #4  
    Donator


    Join Date
    Nov 2008
    Posts
    1,017
    Thanks given
    21
    Thanks received
    32
    Rep Power
    161
    Nice ,,,, Helpful
    Reply With Quote  
     

  5. #5  
    Secret-
    Guest
    I like it,

    but is there a way to restore it by clicking the tray icon.
    Reply With Quote  
     

  6. #6  
    xthexder
    Guest
    Quote Originally Posted by Secret- View Post
    I like it,

    but is there a way to restore it by clicking the tray icon.
    Can't be done unless you make a GUI for the console
    which i haven't done yet
    Reply With Quote  
     

  7. #7  
    Hamachi
    Guest
    Reply With Quote  
     

  8. #8  
    Tierney
    Guest
    convert so i can make lcient run in trey?
    Reply With Quote  
     

  9. #9  
    xthexder
    Guest
    Quote Originally Posted by Clawzz View Post
    convert so i can make lcient run in trey?
    If you want to hide the full client and put it in the tray i won't bother
    the client is only open when your using it, the server is what can get in the way

    if you want your client to not have a console then you use "start javaw" instead of "java" in your run.bat
    i've already put that at the bottom of this though

    I'm working on a GUI system for the console of servers so you can hide/show it
    Reply With Quote  
     

  10. #10  
    Tierney
    Guest
    nice dude
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •