Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree1Likes

Thread: for gimbal2: How to compile from command line and what about the classpath

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default for gimbal2: How to compile from command line and what about the classpath

    Imagine you are at a current directory called "javaworks" and within that folder (dir) there are several Java project folders and one of them is called "serverstuff". So serverstuff is a directory (folder) and a Java project at the same time, that contains two Java source files (with extension .java). The two Java files of the project "serverstuff" contain the line: "package serverstuff;" as first statement! The code of the two classes called A and B should be copied in two files with same names as the classes plus the extension ".java".
    So in short:
    -create a folder called javaworks
    -create inside javaworks a folder called serverstuff
    -put two text-files (text only) with the names: A.java B.java in serverstuff
    -copy the codes at the bottum into the text files (use the option "text only')

    The directory's absolute file-path to the "serverstuff" directory is on my Imac computer: /Users/willem/javaworks
    You can get to that spot, using command line:
    $ cd /Users/willem/javaworks // where cd = current directory
    $ pwd // will show the current directory
    $ ls // will list the current directory
    When you want to move one level up of that directory use:
    $ cd ..


    -First: Use the default classpath that looks for the files at the current directory
    So it is important to navigate to the "javaworks" folder and you do that like this:
    $ cd /Users/willem/javaworks // check with ls or pwd
    Now (from within "javaworks") the first code line will compile, and the second will run the application:
    $ javac serverstuff/A.java serverstuff/B.java (or within the directory serverstuff: javac A.java B.java)
    $ java serverstuff.A // contains the main method (or java serverstuff/A)
    $
    (The source file(s) to be compiled can be one or several listed all together in a row)


    -Second: Now do the same stuff from any spot (dir) of your computer, using a classpath set by -cp "classpath" "file"
    First move out of the dir "javaworks" by typing this:
    cd / // you direct unix to the root directory (check it with ls or pwd)

    javac -cp /Users/willem/javaworks serverstuff/A.java serverstuff/B.java
    java -cp /Users/willem/javaworks serverstuff.A


    -third: Setting temporarily the classpath variable within the bash shell
    Do simply this:
    export CLASSPATH="/Users/willem/javaworks"
    and check the classpath by doing this:
    echo $CLASSPATH
    Now you can type simply this:
    javac serverstuff/A.java serverstuff/B.java // to compile or
    java serverstuff/A // to run the file
    The problem is that once you go out of the bash shell window, the environmental variable will be gone, so it has only a temporal character. In order to have the classpath variable around forever you need to save it.

    -fourth: Saving the classpath environmental variable permanently
    You have to find out where the environmental variables are saved. On my Imac OS 10.6.8 I can not find the file where the environmental variables are stored. I will continue looking but any tip is welcome…..


    Java Code:
    package serverstuff;
    
    /**
     *
     * 
     */
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import javax.swing.*;
    
    /**
     *
     * @author willem
     */
    public class A extends JFrame{
        private B b;
       
        private JButton buttonA;
        private JButton newbutton;
        public A()      {
                JPanel buttonpanel = new JPanel();
                
                b = new B();
                ActionListener blistener = new ButtonListener ();
                setBounds(300, 300, 80, 100);
                setTitle("A-button");
    
    
                buttonA = new JButton("A");
                buttonA.addActionListener(blistener);
                newbutton = b.getButton();
    
                newbutton.addActionListener(blistener);
    
                buttonpanel.add(buttonA); // button of class A
                buttonpanel.add(newbutton); // button of class B
                Container contentPane = getContentPane();         
                contentPane.add(buttonpanel);
        }
    
    
        public class ButtonListener implements ActionListener {// roombuttons
          
           public void actionPerformed(ActionEvent e)  {
               String cmd = e.getActionCommand();
               Object source = e.getSource();
               
                if (cmd.equals("A"))  {
                     System.out.println(" button A pressed ");
                }           
                if (cmd.equals("B"))  { 
                     System.out.println(" remote button B pressed from within class A");
    		 System.out.println(" not really a server yet…");
                }
           }
        }
    
    
        public static void main(String[] args) {
                A a = new A();
                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                a.setVisible(true);
        }
    
    }



    Java Code:
    package serverstuff;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    public class B {
        private JButton buttonB;
    
        public JButton getButton()  {
               buttonB = new JButton("B");
                 
               return buttonB;
        }
        
    }
    Last edited by willemjav; 09-11-2013 at 11:52 PM.

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    I found the bashrc file where should be the classpath variable, right?
    The directory on my mac: /etc/bashrc
    So here I should add the line:
    export CLASSPATH=.:/Users/willem/javaworks
    Strange enough I do not see any other Environment Variables here at this file....
    So i wonder if I am at the right spot?

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Here is another possibility (where are these environment var)

    The .profile File
    The file /etc/profile is maintained by the system administrator of your UNIX machine and contains shell initialization information required by all users on a system. The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes
    • The type of terminal you are using
    • A list of directories in which to locate commands
    • A list of variables effecting look and feel of your terminal. You can check your .profile available in your home directory. Open it using vi editor and check all the variables set for your environment.

    But when I look into it I find this (who can shine a light on this one):

    if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
    fi

    if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
    fi
    ~


    This is BTW the content of /etc/bashrc

    # System-wide .profile for sh(1)

    if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
    fi

    if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
    fi
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    "/etc/profile" [readonly] 9L, 189C

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    what the heck is this stuff? Why are you addressing this directly to me? This is a public forum, not a personal chat room. I refuse to respond to threads that operate like this, sorry.
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    I am sorry to upset you. Gimbal and willemjav are personas, and persona means mask, so we are not in it with our real identity, right!
    When you really are "Just a guy" you could help me to solve that missing link, because I believe my post is very instructive for those who want to learn more about class path which is a tricky subject anyway. You and others could comment on that, right?

    Anyway I'll find it out like I found most things out by myself...

    We are here to learn and some of you have the ability to teach. My learning curve is not a very conventional one for sure.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    I'm not upset, I just refuse to indulge people that have to go against the current. Use forums as they are intended.

    Lets assume you didn't do that - I still can't answer anything because this is just totally unreadable to me. Too much information, too little question.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Thanks gimbal, here is my concrete question (it does not make much sense that you read that stuff because you know it from inside out):

    Where could I find (what file) the environment variables on my mac os 10.6.8?

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Come on man, that's not even a programming question. This is a simple how-to Google question.

    https://www.google.com/#q=mac+environmental+variables
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Thanks gimbal, next time I will try to "behave"....
    I do not read news papers but listen to "alternative media"...
    Might that be the problem?


    BTW I looked into many of those links and it seems to be not that simple.
    But ok I'lll try it again, thanks
    Last edited by willemjav; 09-12-2013 at 11:05 AM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Quote Originally Posted by willemjav View Post
    Thanks gimbal, next time I will try to "behave"....
    I do not read news papers but listen to "alternative media"...
    Might that be the problem?
    ... what does this have to do with anything?

    BTW I looked into many of those links and it seems to be not that simple.
    But ok I'lll try it again, thanks
    You're right, its not simple. Nothing about this job/field is simple and especially not programming.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    I do not agree: "especially not programming"
    I find programming much simpler and logical than all that "surrounding
    environmental stuff"

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    It being logical does not make it easy. You'll figure that out when you do your first real project where you figure out that software development is not about writing code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    -This is my path: /Users/willem/serverstuff
    (within serverstuff are: A.java and B.java)

    -I perform this unix command: cd /Users/willem
    now both command lines work (compile and run)
    javac serverstuff/A.java serverstuff/B.java
    java serverstuff.A

    -To be at the root position I perform this unix command: cd /
    But now the second line is not compiling (javac: file not found: serverstuff/A.java)
    java -cp /Users/willem serverstuff.A
    javac -cp /Users/willem serverstuff/A.java serverstuff/B.java

    why? (man this stuff is really confusing)

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    javac -cp /Users/willem serverstuff/A.java

    Why do you think this will work? This should probable be:

    javac -cp /Users/willem/serverstuff/A.java

    Because you're now compiling using absolute paths while previously you were compiling using relative paths. And these are still not Java questions, you're asking basic Mac usage questions here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Quote Originally Posted by gimbal2 View Post
    javac -cp /Users/willem serverstuff/A.java

    Why do you think this will work? This should probable be:

    javac -cp /Users/willem/serverstuff/A.java

    Because you're now compiling using absolute paths while previously you were compiling using relative paths. And these are still not Java questions, you're asking basic Mac usage questions here.
    Actually, it's:
    javac -cp /Users/willem /Users/willem/serverstuff/A.java

    The first part is the classpath, and the second part is the file you are compiling, which is relative to your current location.
    No need to ask it to compile B.java since the compiler should pick that up.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    -"No need to ask it to compile B.java since the compiler should pick that up.", I noted that already before tolls, thanks

    -I also tried this one already before, gimbal: javac -cp /Users/willem/serverstuff/A.java
    and got (of course): javac: no source files

    -Finally this one compiles: javac -cp /Users/willem /Users/willem/serverstuff/A.java
    Thanks!!!

    But than I thought why not doing this and why you need -cp anyway:
    javac /Users/willem/serverstuff/A.java
    and got: cannot find symbol : class B
    So I did this which compiles too:
    javac /Users/willem/serverstuff/A.java /Users/willem/serverstuff/B.java

    Does it all make sense?
    The classpath -cp points literally as a filepath to the class files to run: fine that makes sense, right!
    But when you think classpath will also point to the java source files than you are wrong (sure you might say it is called CLASSpath).
    So when one uses javac one would like -cp also to point the source files, right?
    Sourcefiles are usually within a different dirctory so one needs a -sp (source path), or a relative path of the source files from the classpath....
    Last edited by willemjav; 09-12-2013 at 04:28 PM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    No, it points to the source files as well (hence why asking it to compile A.java alone results in B.java being compiled as well).

    The classpath is a separate thing that the compiler uses to look up dependencies when compiling the file(s) it has been asked to compile.
    It does not use it to find the files it has been asked to compile.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Quote Originally Posted by Tolls View Post
    Actually, it's:
    javac -cp /Users/willem /Users/willem/serverstuff/A.java

    The first part is the classpath, and the second part is the file you are compiling, which is relative to your current location.
    Argh, I fail! Sharp eyes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    Tolls could you write a line or two more about this:
    "to look up dependencies"

  20. #20
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: for gimbal2: How to compile from command line and what about the classpath

    I still do not understand:
    -when the file path is: /users/willem/serverstuff
    -and Main.java sits in serverstuff
    -I cannot say javac -cp /users/willem/serverstuff Main.java
    I get: javac: file not found: Main.java
    but I can say: javac /users/willem/serverstuff/Main.java


    iMac:serverstuff willem$ pwd
    /Users/willem/serverstuff
    iMac:serverstuff willem$ ls
    A$ButtonListener.class A.java B.java temp
    A.class B.class Main.java
    iMac:serverstuff willem$

    (sorry it is like I am crazy, I'll continue reading)
    Last edited by willemjav; 09-12-2013 at 06:01 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 01-02-2013, 09:50 PM
  2. Replies: 4
    Last Post: 04-06-2012, 09:30 AM
  3. Replies: 16
    Last Post: 09-17-2011, 03:41 AM
  4. Replies: 3
    Last Post: 08-25-2010, 11:21 AM
  5. [SOLVED] Compile from the command line
    By GilaMonster in forum New To Java
    Replies: 8
    Last Post: 06-13-2008, 08:12 AM

Posting Permissions

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