CrazeD
Member
+368|6890|Maine
I'm trying to simultaneously learn Java, while making a program with it. I should note that I don't like taking baby steps, and like to jump right in...which is probably why I'm having problems.

So, yeah. I want a program that will launch COD4 and automatically connect to a server with an IP that I enter into a text box. Sure, I could use XFire or something, but then I'd have to make a favorite everytime. The purpose is for matches/scrims. I normally just copy paste the IP string (which looks like: /connect 1.2.3.4:28960; password asdf) into console and away I go. However, I am lazy.

So: click exe, gives me a text box, I enter that string ^ and it launches COD4 and connects me to the server. I have already done this in C# and it works (albeit slightly buggy). I'd like to learn Java and figured I'd try to port my program.

My problem is that I cannot for the life of me figure out how to make Java launch a program in its own working directory. I can launch the iw3mp.exe, but unless it is launched IN the working directory it will not be able to find the files to load and gives me missing IWD errors. I have Googled for hours, tinkered for hours, can't find a solution.

Code:

import java.io.*;
import javax.swing.JOptionPane;

public class cod4launcher {

    public static void main(String[] args)throws IOException {        
        String serverIP = JOptionPane.showInputDialog(null, "Enter a Server IP!");
        
        if (serverIP != null)
        {            
            
            String file = "F:\\Games\\COD4\\iw3mp.exe";
            Runtime.getRuntime().exec(file);
        }
    }
}
All I want to do at this point is make the game launch. I should be able to handle the rest, but I can't make this work and it's pissing me off. Please help.
Scorpion0x17
can detect anyone's visible post count...
+691|6983|Cambridge (UK)
It's been a long time since I did any Java, and I didn't do much of it when I did, but trying looking for a function, probably in the Runtime object, called something like 'SetWorkingDirectory'.

edit:

I think you need to use System.setProperty("user.dir", <path to directory>).

However, it would appear this function may be a little buggy and so it may not do what you want it to, even though it does, if you see what I mean.

Another option may be to exec() the DOS 'cd' command first, ie:

Code:

...
            Runtime.getRuntime().exec("cd F:\\Games\\COD4\\");
            Runtime.getRuntime().exec("iw3mp.exe");
...

Last edited by Scorpion0x17 (2009-01-16 01:00:04)

-TL-
Srs lurker
+25|6710|Oklahoma City
I may be completely missing the point because I've never used it, and should have gone to sleep 3 hours ago, but wouldn't adding a few more parameters to the .exec() call be what you're looking for?

Link to the Java API for what I mean: http://java.sun.com/javase/6/docs/api/j … .html#exec  (The fourth exec() down).
Scorpion0x17
can detect anyone's visible post count...
+691|6983|Cambridge (UK)

-TL- wrote:

I may be completely missing the point because I've never used it, and should have gone to sleep 3 hours ago, but wouldn't adding a few more parameters to the .exec() call be what you're looking for?

Link to the Java API for what I mean: http://java.sun.com/javase/6/docs/api/j … .html#exec  (The fourth exec() down).
Well spotted, or remembered, or known, sir.

However, isn't it the 6th one down that he wants?

(the fourth one taking an array of command strings, rather then just a single command string)
-TL-
Srs lurker
+25|6710|Oklahoma City
Yeah, it probably is, I just looked up runtime.exec() when he asked and saw that it has a parameter for the working directory.

Last edited by -TL- (2009-01-16 01:18:49)

CrazeD
Member
+368|6890|Maine

Scorpion0x17 wrote:

-TL- wrote:

I may be completely missing the point because I've never used it, and should have gone to sleep 3 hours ago, but wouldn't adding a few more parameters to the .exec() call be what you're looking for?

Link to the Java API for what I mean: http://java.sun.com/javase/6/docs/api/j … .html#exec  (The fourth exec() down).
Well spotted, or remembered, or known, sir.

However, isn't it the 6th one down that he wants?

(the fourth one taking an array of command strings, rather then just a single command string)
Yeah it appears to be. I've been to that page, many a time. I cannot get it to work though. An example or something would be awesome, but I can't find one. >.<
Beduin
Compensation of Reactive Power in the grid
+510|5967|شمال
Fucking hate Java
الشعب يريد اسقاط النظام
...show me the schematic
ghettoperson
Member
+1,943|6866

Beduin wrote:

Fucking hate Java
Ditto. Interesting project though. How are you going to give the server IP to the program though?
CrazeD
Member
+368|6890|Maine

ghettoperson wrote:

Beduin wrote:

Fucking hate Java
Ditto. Interesting project though. How are you going to give the server IP to the program though?

Code:

String serverIP = JOptionPane.showInputDialog(null, "Enter a Server IP!");
ghettoperson
Member
+1,943|6866

That's just how you're assigning the IP to a variable. How do you input that variable into the program?
CrazeD
Member
+368|6890|Maine
It is a command line parameter.
ghettoperson
Member
+1,943|6866

Can you post up the code in C and I might be able to figure it out.
CrazeD
Member
+368|6890|Maine
Well, there's a helluva lot more code ... but sure.

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace COD4Connect3
{    
    public partial class COD4Connect : Form
    {
        ArrayList servers = new ArrayList();
        public COD4Connect()
        {
            InitializeComponent();
            checkServerFile();
            getServerList();
            populateList(servers);
            checkUniteSniper();
            checkXray();
        }

        private String getPath()
        {
            String path;
            try
            {
                RegistryKey regkey;

                regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Activision\Call of Duty 4\");
                path = (string)regkey.GetValue("InstallPath");

                return path;
            }
            catch (Exception)
            {
                MessageBox.Show("Error! Could not get InstallPath from registry!");
                return null;
            }
        }

        private void checkUniteSniper()
        {
            var getFile = new FileInfo(getPath() + "mods\\pam4\\zzz_unitesniper.iwd");
            if (getFile.Exists == true)
            {
                deleteus.Enabled = true;
            }
            else
            {
                deleteus.Enabled = false;
            }
        }

        private void deleteUniteSniper()
        {
            var getFile = new FileInfo(getPath() + "mods\\pam4\\zzz_unitesniper.iwd");
            if (getFile.Exists == true)
            {
                getFile.Delete();
                deleteus.Enabled = false;
                MessageBox.Show("\'zzz_unitesniper.iwd\' Has been deleted!");
            }
        }

        private void launchCOD4(String serverip)
        {
            String[] temp;
            String serverpw;

            if (serverip != "")
            {

                serverip = serverip.Trim();

                temp = serverip.Split(new char[] { ';' });

                serverip = temp[0];
                serverpw = temp[1];

                serverip = serverip.Replace("/", "");
                serverip = serverip.Trim();
                serverpw = serverpw.Trim();


                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.FileName = "iw3mp.exe";
                startInfo.Arguments = "+" + serverip + " +" + serverpw;
                startInfo.WorkingDirectory = getPath();
                startInfo.WindowStyle = ProcessWindowStyle.Maximized;

                Process.Start(startInfo);

                Application.Exit();
            }
        }

        private void getServerList()
        {
            StreamReader getfile = File.OpenText("cod4connectServerlist.txt");
            String getline;
            while ((getline = getfile.ReadLine()) != null)
            {
                servers.Add(getline);
            }
            getfile.Close();
        }

        private void populateList(ArrayList list)
        {
            String[] split;
            foreach (String temp in list)
            {
                split = temp.Split(new char[] { '|' });
                serverlist.Items.Add(split[0].Trim());
            }
        }

        private void rePopulateList()
        {
            serverlist.Items.Clear();
            populateList(servers);
        }

        private void addToList(String item)
        {
            if (item != "")
            {
                servers.Add(makeServerString(serverip.Text, servername.Text));
                rePopulateList();
            }
        }

        private void remFrList(int index)
        {
            if (index != -1)
            {
                servers.RemoveAt(index);
                rePopulateList();
                remFrFile();
                serverip_rem.Enabled = false;

            }
        }

        private void addToFile(String serverstring)
        {
            StreamWriter appendFile;
            appendFile = File.AppendText("cod4connectServerlist.txt");
            appendFile.WriteLine(serverstring);
            appendFile.Close();
        }

        private void remFrFile()
        {
            TextWriter makeList = new StreamWriter("cod4connectServerlist.txt");

            foreach (String temp in servers)
            {
                makeList.WriteLine(temp);
            }
            makeList.Close();
        }

        private void toggleAdd()
        {
            if (servername.Enabled == false)
            {
                servername.Enabled = true;
                serverip_add.Enabled = true;
            } else
                if (servername.Enabled == true)
                {
                    servername.Enabled = false;
                    serverip_add.Enabled = false;
                    servername.Text = "";
                }
        }

        private String makeServerString(String serverip, String servername)
        {
            String[] temp;
            String serverpw;

            temp = serverip.Split(new char[] { ';' });

            serverip = temp[0];
            serverpw = temp[1];

            serverip = serverip.Replace("/", "");
            serverip = serverip.Replace("connect", "");
            serverip = serverip.Trim();

            serverpw = serverpw.Trim();
            serverpw = serverpw.Replace("password ", "");
            serverpw = serverpw.Trim();

            String serverString = servername + " | " + serverip + " | " + serverpw;
            serverString = serverString.Trim();

            return serverString;
        }

        private void checkXray()
        {
            Process[] process = Process.GetProcessesByName("xac");
            if (process.Length == 0)
            {
                xray_status.Text = "X-Ray is not running!";
            }
            else
            {
                xray_status.Text = "X-Ray is running!";
            }
        }

        private void checkServerFile()
        {
            var getFile = new FileInfo("cod4connectServerlist.txt");
            if (getFile.Exists == false)
            {
                createServerFile();
            }

        }

        private void createServerFile()
        {
            StreamWriter createFile = File.CreateText("cod4connectServerlist.txt");
            createFile.Close();
        }

        private String getIPPW()
        {
            if (serverlist.SelectedIndex != -1)
            {
                String[] temp;
                String serverip;
                String serverpw;
                String serverString;

                temp = servers[serverlist.SelectedIndex].ToString().Split(new char[] { '|' });

                serverip = temp[1];
                serverpw = temp[2];

                serverip = serverip.Trim();
                serverpw = serverpw.Trim();

                serverString = "/connect " + serverip + "; password " + serverpw;

                return serverString;
            }
            else
            {
                return null;
            }
        }

        private void serverip_add_Click(object sender, EventArgs e)
        {
            addToList(servername.Text);
            addToFile(makeServerString(serverip.Text, servername.Text));
        }

        private void serverip_rem_Click(object sender, EventArgs e)
        {
            remFrList(serverlist.SelectedIndex);
            remFrFile();
        }   

        private void serverlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (serverlist.SelectedIndex != -1)
            {
                serverip.Text = getIPPW();
                serverip_rem.Enabled = true;
            }
        }

        private void connect_Click(object sender, EventArgs e)
        {
            launchCOD4(serverip.Text);
        }

        private int checkForExistingServer(String serverip)
        {    
            int count = servers.Count;
            String[] temp;
            String serverString;
            for (int i = 0; i < count; i++)
            {
                temp = servers[i].ToString().Split(new char[] { '|' });
                serverString = "/connect " + temp[1].Trim() + "; password " + temp[2].Trim();
                if (serverString == serverip)
                {   
                    return 1;
                }
            }
            return 0;
        }

        private void serverip_TextChanged(object sender, EventArgs e)
        {
            if (serverip.Text.IndexOf(";") != -1)
            {
                if (checkForExistingServer(serverip.Text) == 0)
                {
                    addserver.Enabled = true;
                }
                else
                {
                    addserver.Enabled = false;
                    addserver.Checked = false;
                }
            }
        }

        private void addserver_CheckedChanged(object sender, EventArgs e)
        {
            toggleAdd();
        }

        private void deleteus_Click(object sender, EventArgs e)
        {
            deleteUniteSniper();
        }            
    }
}
Relevant part:

Code:

 private void launchCOD4(String serverip)
        {
            String[] temp;
            String serverpw;

            if (serverip != "")
            {

                serverip = serverip.Trim();

                temp = serverip.Split(new char[] { ';' });

                serverip = temp[0];
                serverpw = temp[1];

                serverip = serverip.Replace("/", "");
                serverip = serverip.Trim();
                serverpw = serverpw.Trim();


                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.FileName = "iw3mp.exe";
                startInfo.Arguments = "+" + serverip + " +" + serverpw;
                startInfo.WorkingDirectory = getPath();
                startInfo.WindowStyle = ProcessWindowStyle.Maximized;

                Process.Start(startInfo);

                Application.Exit();
            }
        }
It's C# by the way, not C.
ghettoperson
Member
+1,943|6866

Oh, so you can just add arguments to the end of the file and it'll log you in and go to an IP? I didn't realise that. That was just what I wanted to know how you were doing.

EDIT: I have no idea what the difference between C and C# is, I can't write anything in either, but I know enough Java to be able to figure out most other languages. Apart from Brainfuck...

Last edited by ghettoperson (2009-01-16 16:58:25)

CrazeD
Member
+368|6890|Maine
Yes, they're just command line parameters. "iw3mp.exe +connect 1.1.1.1 +password blahblah"

Anyways... I haven't got that far yet. All I want right now is to make the damn game start at all.

EDIT: C is sort of the predecessor of C++, though not officially. C# is a language made by Microsoft to sort of mimic Java, and uses the .NET Framework.

If you can help me get the Runtime working directory thing sorted, that'd be sweet. All I'm looking to do at this point is to simply start the game. It doesn't have to connect to a server, just start.

Last edited by CrazeD (2009-01-16 17:00:35)

CrazeD
Member
+368|6890|Maine
Bump.

I've made a little progress, I think.

Code:

String workingDir = "F:\\Games\\COD4";
String cmd = "iw3mp.exe";            
            
Runtime.getRuntime().exec(cmd,null,new File(workingDir));
When I do this, I get this error:

Exception in thread "main" java.io.IOException: Cannot run program "iw3mp.exe" (in directory "F:\Games\COD4"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at cod4launcher.main(cod4launcher.java:29)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 4 more
Why is the file not found?

Last edited by CrazeD (2009-01-18 11:49:57)

ghettoperson
Member
+1,943|6866

I hate Java like that, it always throws some error like that, and then the problem usually turns out to be completely unrelated and you missed a semi-colon somewhere miles away from where it throws the error.

Don't know tbh, did you try what Scorpion said with using "cd 'filepath' "?
CrazeD
Member
+368|6890|Maine
Yes I did, it didn't work.

I don't know why it is so complicated for such a simple fucking task.
ghettoperson
Member
+1,943|6866

Cause Java's a bitch. I don't know anyone who likes Java.
Finray
Hup! Dos, Tres, Cuatro
+2,629|6006|Catherine Black
Holy shit you guys are geeky.
https://i.imgur.com/qwWEP9F.png
jsnipy
...
+3,276|6740|...

lol how was it buggy in C#
CrazeD
Member
+368|6890|Maine
Well for one, if you don't have COD4 installed it totally freaks out.

And if you do certain things, it totally freaks out.

Basically my lack of exception handling, really. It works fine if you don't try to break it.
Scorpion0x17
can detect anyone's visible post count...
+691|6983|Cambridge (UK)
Craze, I've no idea what the problem is, but just thought I'd correct you on one thing:

CrazeD wrote:

EDIT: C is sort of the predecessor of C++, though not officially.
C was the predecessor to C++, officially - I went to a very interesting lecture given by the bloke (who's name I forget) that invented C++ and it started out as pure C (using fancy preprocessor commands to add the OO features).

CrazeD wrote:

Well for one, if you don't have COD4 installed it totally freaks out.

And if you do certain things, it totally freaks out.

Basically my lack of exception handling, really. It works fine if you don't try to break it.
Welcome to the Microsoft Software Development Team!

Last edited by Scorpion0x17 (2009-01-18 15:44:32)

CrazeD
Member
+368|6890|Maine

Scorpion0x17 wrote:

CrazeD wrote:

Well for one, if you don't have COD4 installed it totally freaks out.

And if you do certain things, it totally freaks out.

Basically my lack of exception handling, really. It works fine if you don't try to break it.
Welcome to the Microsoft Software Development Team!


Well, it works fine for my use, cause it was coded for my conditions.

Would need some patchwork if I were to distribute it, but meh... I don't really care, I'm lazy.
jsnipy
...
+3,276|6740|...

Scorpion0x17 wrote:

CrazeD wrote:

Well for one, if you don't have COD4 installed it totally freaks out.

And if you do certain things, it totally freaks out.

Basically my lack of exception handling, really. It works fine if you don't try to break it.
Welcome to the Microsoft Software Development Team!
erm has nothing to do with M$

Board footer

Privacy Policy - © 2024 Jeff Minard