PDA

View Full Version : How would I do this in a .command file?




iLambo
Mar 6, 2012, 06:36 PM
Okay, so I have a friend who is developing an online game and needs java to run.

It is fully done on windows as a .bat file and it has a .jar file.

Now, when I would like to open it on mac, the .Jar will not work.

If I would like to actually use it I have to do this...


1. Open Terminal
2. type "cd (drag file here)"
3. Enter the text "java -Xmx512m Gui" and press enter.
4. It is now open.

What I would like to do is add a .command file or any other type of file that will do it for you. All you will have to do is double-click on it and it will open on its own.

I believe if I put the .command file into the folder, it will automatically have the Directory Changed.

I just need to know how to make it run the client by having it automatically enter "java -Xmx512m Gui".

How can I do this?

Thanks!



wrldwzrd89
Mar 7, 2012, 06:59 AM
You are correct on the first part, so you don't need the first command for changing directories. All you need is this, in a text file with the command extension:
#!/bin/bash
java -Xmx512m Gui &
exit

I THINK that'll work, and it will even close the Terminal window for you. Don't forget to give the start.command (or whatever) executable permission:
chmod +x (drag file to Terminal window)

iLambo
Mar 7, 2012, 02:59 PM
You are correct on the first part, so you don't need the first command for changing directories. All you need is this, in a text file with the command extension:
#!/bin/bash
java -Xmx512m Gui &
exit

I THINK that'll work, and it will even close the Terminal window for you. Don't forget to give the start.command (or whatever) executable permission:
chmod +x (drag file to Terminal window)

No luck. I added that. It was called "run.command" and it had that first code only.

I then did "chmod +x (.command file here)" and pressed enter.


When I ran it with terminal, I got this...

Last login: Wed Mar 7 15:45:32 on ttys000
MacBook-Pro:~ Jamie$ /Users/Jamie/Desktop/client/run.command ; exit;
logout

[Process completed]

Any other solution?

chown33
Mar 7, 2012, 03:24 PM
1. Open Terminal
2. type "cd (drag file here)"
3. Enter the text "java -Xmx512m Gui" and press enter.
4. It is now open.


Line 3 looks wrong. I don't see anything that tells the java command what jar-file to use. So assuming you actually have a jar-file, then the Gui class won't be found.

However, if you actually have loose class files, i.e. Gui.class and so on, then you must make sure that the command-file is in the same directory as Gui.class.

If you really have a jar-file, then you need to specify it in the command, like so:
java -Xmx512m -cp path/to/Your.jar Gui



Frankly, a double-clickable jar should work. It definitely will work on any OS version before 10.7 Lion. On Lion, I don't know.

A double-clickable jar file is any jar containing a manifest with a line like this:
Main-Class: fully.qualified.ClassName

You can discover whether a jar contains a manifest with this command line:
jar tf path/to/Your.jar

If it shows META-INF/MANIFEST.MF then it has a manifest. Whether the manifest has a Main-Class entry or not, you should start by asking your friend. If your friend doesn't know what a manifest file is, have him google it (java manifest), and figure out how to set the main-class in whatever development system he's using.


By the way, it's pointless to try making a command-file for a command-line that doesn't work. I really don't see how the command-line you posted could possibly work, since it doesn't name the jar file.

You need to look at the error message when the command fails. If you do that, by pasting your original command into Terminal, I would bet that an error message appears. Post that error message.

Until you get the command-line itself correct, putting it into a double-clickable command-file won't do anything.

cqexbesd
Mar 7, 2012, 03:27 PM
My first thought would be you shouldn't need a script at all - I would have thought you could have just double clicked it in the finder. Personally I avoid Java when I can so I haven't done it myself but perhaps Google will help. "Jar Launcher" is probably what you need.

If you did want it to use a script then if the jar file has a proper manifest you should be able to use:

java -jar /path/to/foo.jar

and if you don't have a correct manifest then:

java -cp /path/to/foo.jar MainClass

(substituting paths and class names as appropriate).

I haven't tested any of that but hopefully it points you in the right direction.

wrldwzrd89
Mar 7, 2012, 03:30 PM
No luck. I added that. It was called "run.command" and it had that first code only.

I then did "chmod +x (.command file here)" and pressed enter.


When I ran it with terminal, I got this...

Last login: Wed Mar 7 15:45:32 on ttys000
MacBook-Pro:~ Jamie$ /Users/Jamie/Desktop/client/run.command ; exit;
logout

[Process completed]

Any other solution?
Okay. I experimented with things a bit, and finally got something that works.

#!/bin/bash
cd /path/to/Gui.jar
java -jar -Xmx512m Gui.jar &
killall Terminal

It's an ugly hack, 'cause it'll kill any other Terminal sessions you have open... but at least the Terminal window will go away. If you want to see error messages and output from the 2nd command... change your script to this:

#!/bin/bash
cd /path/to/Gui.jar
java -jar -Xmx512m Gui.jar & > ~/Desktop/output.txt 2> ~/Desktop/errors.txt
killall Terminal

The output and errors will be in the two text files on your Desktop.

chown33
Mar 7, 2012, 04:21 PM
Okay. I experimented with things a bit, and finally got something that works.

#!/bin/bash
cd /path/to/Gui.jar
java -jar -Xmx512m Gui.jar &
killall Terminal

That seems ..., well, improbable.

First, I doubt that you can cd to a jar-file. You could cd to the directory that contains the jar-file, but not to the actual jar-file.

Second, although it might work, it's unwise to split the options like that. The Gui.jar belongs to the -jar option, not to the -Xmx option. I haven't tried that, so it could work, but it would still be unwise.

Third, jar-files typically don't care what their working directory is. So if the goal is simply to run the jar-file, you can avoid the cd with this:
java -Xmx512m -jar /path/to/Gui.jar

Finally, look at the nohup command, which is there so you can keep something running after the shell terminates. So all together:
nohup java -Xmx512m -jar /path/to/Gui.jar &>/dev/null &

Now you should be able to quit Terminal and the program will keep running.

Or just fix the manifest in the jar-file. It's possible to do this with a jar command-line, but I'd have to dig into 'man jar' and figure out how to update the jar with a new manifest, assuming it doesn't have one.

Because making the jar-file itself double-clickable is going to be a lot better than saying "keep this command-file with the jar-file, and edit it like so to make sure it tracks things when you move it around on your disk".

wrldwzrd89
Mar 7, 2012, 04:31 PM
That seems ..., well, improbable.

First, I doubt that you can cd to a jar-file. You could cd to the directory that contains the jar-file, but not to the actual jar-file.

Second, although it might work, it's unwise to split the options like that. The Gui.jar belongs to the -jar option, not to the -Xmx option. I haven't tried that, so it could work, but it would still be unwise.

Third, jar-files typically don't care what their working directory is. So if the goal is simply to run the jar-file, you can avoid the cd with this:
java -Xmx512m -jar /path/to/Gui.jar

Finally, look at the nohup command, which is there so you can keep something running after the shell terminates. So all together:
nohup java -Xmx512m -jar /path/to/Gui.jar &>/dev/null &

Now you should be able to quit Terminal and the program will keep running.

Or just fix the manifest in the jar-file. It's possible to do this with a jar command-line, but I'd have to dig into 'man jar' and figure out how to update the jar with a new manifest, assuming it doesn't have one.

Because making the jar-file itself double-clickable is going to be a lot better than saying "keep this command-file with the jar-file, and edit it like so to make sure it tracks things when you move it around on your disk".
Huh, I didn't know about the nohup command. Thanks!
You are right about updating the manifest inside the JAR file - that would be a LOT simpler than this command line method.

iLambo
Mar 7, 2012, 04:47 PM
Wrld, the actual file it tells to open is called gui.class.

No cd is needed because the run.command is in the folder.

Basically all I need it to do is run the "java (whatever) GUI".

I always do this but I have to enter it manually. This will be distributed to hundreds of people. I don't want them to have to do it all manually because they can be pretty stupid...

chown33
Mar 7, 2012, 05:02 PM
Wrld, the actual file it tells to open is called gui.class.

Then it's not in a jar file.

Get the jar file. The same one you said your friend used on Windows. If you try it and it doesn't work, then post exactly what you did, and exactly what error messages you got.

There should be no reason that a jar file won't work on Mac OS X. If it's not working, then we need to figure out exactly why not. We can't do that if we don't know exactly what you tried, and exactly what the results were, including the exact error messages.

Distributing loose class files is the worst possible distribution method.

iLambo
Mar 7, 2012, 05:06 PM
Then it's not in a jar file.

Get the jar file. The same one you said your friend used on Windows. If you try it and it doesn't work, then post exactly what you did, and exactly what error messages you got.

There should be no reason that a jar file won't work on Mac OS X. If it's not working, then we need to figure out exactly why not. We can't do that if we don't know exactly what you tried, and exactly what the results were, including the exact error messages.

Distributing loose class files is the worst possible distribution method.

I'll post that later when I have my computer in front of me.

You don't get what I mean by a class file. It's a run.bat for windows. In the run.bat it tells the computer what to open and what to use to open it. It just happens to be saying "Use java and open GUI.class".

chown33
Mar 7, 2012, 05:10 PM
You don't get what I mean by a class file. It's a run.bat for windows. In the run.bat it tells the computer what to open and what to use to open it. It just happens to be saying "Use java and open GUI.class".

I know exactly what a class file is. And what a .bat file is.

If the batch file is referring to loose class files, then your friend is not using a jar file on Windows. Your first post said "jar" twice, so when it later turned out you had loose class files, you should expect people not to have realized that fact.

Please post the actual contents of the Windows .bat file, i.e. copy and paste it into a post. It often helps to see the existing command, in order to make an equivalent Mac OS X command line. Otherwise we're all just guessing about what you actually have.

iLambo
Mar 7, 2012, 05:17 PM
I know exactly what a class file is. And what a .bat file is.

If the batch file is referring to loose class files, then your friend is not using a jar file on Windows. Your first post said "jar" twice, so when it later turned out you had loose class files, you should expect people not to have realized that fact.

Please post the actual contents of the Windows .bat file, i.e. copy and paste it into a post. It often helps to see the existing command, in order to make an equivalent Mac OS X command line. Otherwise we're all just guessing about what you actually have.

Okay I'll post it all later.

When I was talking about a .jar, I was meaning he took this all, use Jar-Maker and it's able to be used perfectly on windows. When used on a Mac, it says "Error Loading... please report" or something close to that.

chown33
Mar 7, 2012, 05:43 PM
Okay I'll post it all later.

When I was talking about a .jar, I was meaning he took this all, use Jar-Maker and it's able to be used perfectly on windows. When used on a Mac, it says "Error Loading... please report" or something close to that.

Please identify exactly what this Jar-Maker program is. Googling Jar-Maker shows at least two different products.

If you're trying to run a jar file on a Mac, we'll need to know the exact error message. Accuracy is important in programming.

We'll also need to know what your OS version is. Again, accuracy is important.

iLambo
Mar 7, 2012, 06:33 PM
Please identify exactly what this Jar-Maker program is. Googling Jar-Maker shows at least two different products.

If you're trying to run a jar file on a Mac, we'll need to know the exact error message. Accuracy is important in programming.

We'll also need to know what your OS version is. Again, accuracy is important.

http://download.cnet.com/JAR-Maker/3000-2213_4-10588876.html - Jar-Maker

@echo off
Title Client
START java -Xmx512m Gui 30 0 lowmem members 32
exit

^^^^ Original Run.bat ^^^^

When running the Client.Jar it will load but then stop and say

Error Loading...Please Report!

chown33
Mar 7, 2012, 08:48 PM
http://download.cnet.com/JAR-Maker/3000-2213_4-10588876.html - Jar-Maker

@echo off
Title Client
START java -Xmx512m Gui 30 0 lowmem members 32
exit

^^^^ Original Run.bat ^^^^


That's not using a jar-file. So maybe the Windows version has loose class files, too.


When running the Client.Jar it will load but then stop and say

Error Loading...Please Report!
How are you running the Client.jar, exactly?

From a command-line? If so, give the exact command line. Select the lines in Terminal, copy and paste into a post.

From a command-file script? If so, post the complete exact script.

From a double-click in Finder? If so, describe where "Error Loading...Please Report" appears. Is it in a dialog or alert? Post a screen shot.


Is it possible for you to put the Client.jar and whatever.bat on a website like pastebin, then post the URL here? So someone else can actually try these things you're doing, and attempt to replicate the errors.