Java object file won't run "command not found"

Discussion in 'Mac Programming' started by tmscott, Jan 5, 2010.

  1. tmscott macrumors newbie

    Joined:
    Jan 5, 2010
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Unless you have "." added to your PATH, you may need to run it as "./hoppity"
     
  3. tmscott thread starter macrumors newbie

    Joined:
    Jan 5, 2010
    #3
    Hmm, still didn't work!

    ./hoppity test
    -bash: ./hoppity: Permission denied
    sudo ./hoppity test
    sudo: ./hoppity: command not found
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Might need to set permissions:
    Code:
    chmod 755 hoppity
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Oops, yea lee1210's right, I forgot about that :)
     
  7. tmscott thread starter macrumors newbie

    Joined:
    Jan 5, 2010
    #7
    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...
     
  8. tmscott thread starter macrumors newbie

    Joined:
    Jan 5, 2010
    #8
    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!
     
  9. pikester macrumors newbie

    pikester

    Joined:
    Feb 27, 2009
    #9
    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.
     

Share This Page