Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Running a class outside the directory

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Running a class outside the directory

    Hi everyone!

    I have a directories in UNIX:
    /home/t_bmf/Java/HelloWorld/src/helloworld :will contain a .java file
    /home/t_bmf/Java/HelloWorld/bin :will contain all .class file

    let say a have a code:
    package helloworld;
     
    public class HelloWorld {
     
          public static void main(String[] arg) {
                System.out.println("Hello World");
          }
    }

    a command to compile this even outside the directory /home/t_bmf/Java/HelloWorld/src/helloworld
    javac -d /home/t_bmf/Java/HelloWorld/bin /home/t_bmf/Java/HelloWorld/src/helloworld/HelloWorld.java

    this will generate a directory /home/t_bmf/Java/HelloWorld/bin/helloworld
    and file inside this is HelloWorld.class

    to run this program I must be in directory /home/t_bmf/Java/HelloWorld/bin and using this command:
    java helloworld.HelloWorld

    Question:
    I already how to run the HelloWorld.class, but I must be in helloworld /home/t_bmf/Java/HelloWorld/bin
    to run it.
    Is there's a way to run the class even when I am not in directory /home/t_bmf/Java/HelloWorld/bin?
    let's say I'm in /home/t_bmf, can I still run the HelloWorld.class?

    Thank you so much!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,084
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: Running a class outside the directory

    Yes. This is a Unix question, not a Java question.

  3. #3
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Running a class outside the directory

    Yes, you can do so by setting the classpath i guess.

  4. The Following User Says Thank You to ankurt For This Useful Post:

    dicdic (February 17th, 2014)

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Running a class outside the directory

    Yes, you can do so by setting the classpath i guess.
    Thanks, I made it now!
    using this command:
    java -classpath /home/t_bmf/Java/HelloWorld/bin helloworld.HelloWorld

    Thanks for the help!

  6. #5
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Running a class outside the directory

    Anytime

  7. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Running a class outside the directory

    BTW do you know what is the difference between $classpath and -classpath? the command doesn't work with $classpath.
    so I guess they are different.

    I hope I can open IDE from our server.

  8. #7
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Running a class outside the directory

    I am not sure but according to me -classpath is a command to set the classpath and $CLASSPATH is the environment variable which contains the value of classpath.

    Do confirm, not 100% sure.

    --- Update ---

    to check your current set classpath, you use

    echo $CLASSPATH

    and if it returns a blank line then it means nothing is set as the classpath.

    CLASSPATH=/home/test/class
    java -cp $CLASSPATH program

  9. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Running a class outside the directory

    Do confirm, not 100% sure.
    I knew it now.
    $CLASSPATH is just a variable just like String variables in programming languages.

    in my experiment i noticed that i can also create another environment variable just like:
    me=/home/t_bmf/

    so I can use this variable me whenever I compile or run a program just like:
    java -cp $me package.program

    I also noticed that to be able to used that environment variable, we need to
    include -cp to the command.

    Thanks ankurt for your help. this is really helpful.
    actually I'm creating a java program that will automatically compile and run another
    java program dynamically provided that it has source packages(for .java files), bin packages(for .class files)
    and lib folder for jar files(optional). And this must be dynamic since the program cannot tell how many packages
    the other program has.
    because there is no IDE in our server
    I hope you can help me again in my next thread.. Thanks a lot!

  10. #9
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Running a class outside the directory

    Definitely. Everyone here is there to help everyone else.

Similar Threads

  1. Problem with running random class
    By Gizken in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 27th, 2012, 06:50 AM
  2. Running methods of a class that created you...
    By joestr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 3rd, 2011, 02:59 PM
  3. Help with running class file
    By carface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 30th, 2010, 05:40 PM
  4. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM
  5. Class directory
    By Kit in forum Java Theory & Questions
    Replies: 7
    Last Post: October 11th, 2009, 11:07 AM