Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tmscott

macrumors newbie
Original poster
Jan 5, 2010
4
0
Hi,

I'm developing from my unix terminal and I have a makefile that creates an object file for a java program. I can execute the object file on my school server, but i cant get it to work on my new macbook pro. Here's my makefile:

2 JAVASRC = hoppity.java
3 SOURCES = ${JAVASRC} Makefile
4 MAINCLASS = hoppity
5 CLASSES = hoppity.class
6 JARCLASSES = ${CLASSES}
7 JARFILE = hoppity
8
9 all: ${JARFILE}
10
11 ${JARFILE}: ${CLASSES}
12 echo Main-Class: ${MAINCLASS} >Manifest
13 jar cvfm ${JARFILE} Manifest ${JARCLASSES}
14 -rm Manifest
15 chmod +x ${JARFILE}
16
17 %.class: %.java
18 javac $<
19
20

After i make, i should be able to type in 'hoppity' and run the program from the command line, however i'm getting the following error:

-bash: hoppity: command not found

Is this a linker or classpath problem? Do i need to export something in my .bash_profile?

Thanks
 
Unless you have "." added to your PATH, you may need to run it as "./hoppity"
 
Hmm, still didn't work!

./hoppity test
-bash: ./hoppity: Permission denied
sudo ./hoppity test
sudo: ./hoppity: command not found
 
makefile:
Code:
JAVASRC = hoppity.java
SOURCES = ${JAVASRC} Makefile
MAINCLASS = hoppity
CLASSES = hoppity.class
JARCLASSES = ${CLASSES}
JARFILE = hoppity

all: ${JARFILE}

${JARFILE}: ${CLASSES}
        echo Main-Class: ${MAINCLASS} >Manifest
        jar -cvfm ${JARFILE}.jar Manifest ${JARCLASSES}
        rm Manifest
        mv ${JARFILE}.jar ${JARFILE}
        chmod +x ${JARFILE}

%.class: %.java
        javac $<

hoppity.java:
Code:
public class hoppity {
  static public void main(java.lang.String args[]) {
    System.out.println("This is hoppity. Word up.");
  }
}

Commands run:
Code:
make all
java hoppity

Maybe it's possible, but i have never run a java program (without a supporting shell script, etc.) without running "java myProg". As such, i've never set a .jar file to executable. I left that in since you seemed to be making use of it, but again, i'm not sure how.

-Lee
 
Might need to set permissions:
Code:
chmod 755 hoppity

Thanks that worked! So do i have to set permissions for every object file that i generate? I guess i could do that in the Makefile...
 
Yea Lee is right. I'm going to have to ask my professor about that one...i wonder why it works on my school server.

I can use a different approach. The makefile that worked was:

2 JAVAC = javac
3 hoppity.class: hoppity.java Makefile
4 $(JAVAC) $<

and then i simply created a file named hoppity that contained

java hoppity $1

and finally i was able to run the program by simply typing in 'hoppity <filename>"

thanks again!
 
Using a Makefile to compile one Java file does seem to be a bit of overkill. Here is a very simple way to do it:

Hello.java:
Code:
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello " + args[0]);
  }
}

javac Hello.java
java -cp . Hello tmscott

If you start to look at working with larger code sets and packages for your code, you will want to look at tools like Ant ( http://ant.apache.org/ ) or Maven ( http://maven.apache.org/ ), they are both like Make, but geared towards Java and each has a 10 minute tutorial to make coding in Java much, much easier. Maven is a great way to generate a Jar file extremely easily from a set of code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.