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 4 of 4

Thread: Running Program in UNIX with package

  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 Program in UNIX with package

    Hi everyone

    let's say i have a program in UNIX directory /home/me/java/src
    package bin;
     
    public class HelloWorld {
           public static void main(String[] args) {
                   System.out.println("Hello World");
           }
    }

    I have manage to compile it successfully(class file will be in bin directory) using command below:
    javac -d /home/t_bmf/java/bin HelloWorld.java

    My problem now is how would I execute the class in bin directory in UNIX?
    I already tried different ways on how to execute it as suggested in my internet research
    The one I tried is this:
    java $CLASSPATH:.:bin/HelloWorld
    but I got this error message:
    Exception in thread "main" java.lang.NoClassDefFoundError: :/:bin/HelloWorld
    Caused by: java.lang.ClassNotFoundException: :.:bin.HelloWorld
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    Could not find the main class: :.:bin/HelloWorld. Program will exit.


    Thank you for your Help!..


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

    Default Re: Running Program in UNIX with package

    After compiling try going to the directory java, and from there execute using

    java bin.HelloWorld


    It might work

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

    Default Re: Running Program in UNIX with package

    thank you ankurt
    it works now..
    so it means that the way to run a program with package is through its parent directory right?
    well i hope i can do it together with jar files..
    I just need to add the -cp and $CLASSPATH right?
    Thank you so much for your time..

    --- Update ---

    Hi again..
    i have bin, src and lib directory inside Java directory which must contains:
    bin all compiled programs (.class)
    src all program source (.java)
    bin all jar files

    I can only compile a program together with jar files if those jar files are in same directory of .java files
    this is the command that i used
    javac -cp $classpath:.:first.jar:second.jar -d /home/me/Java HelloWorld.java
    but i don't want this. I want the jar files to be located only in lib directory.
    I tried this command:
    javac -cp $CLASSPATH:.:home/t_bmf/Java/lib/first.jar:home/t_bmf/.Java/lib/second.jar -d /home/t_bmf/Java TestJar.java
    but i got this error:
    TestJar.java:3: package firstjar does not exist
    import firstjar.FirstJarPrint;
    ^
    TestJar.java:4: package secondjar does not exist
    import secondjar.SecondJar;
    ^
    2 errors


    I'm sorry, I'm just new in UNIX. i usually do programs in windows and i can only access our server remotely..
    Thank you for your time..

    --- Update ---

    I made it now.. using this command
    javac -cp ".:/home/t_bmf/Java/lib/FirstJar.jar:/home/t_bmf/Java/lib/SecondJar.jar" -d /home/t_bmf/Java TestJar.java
    Thank you..

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

    Default Re: Running Program in UNIX with package

    Great. You have started solving your problems yourself...

Similar Threads

  1. [SOLVED] Compiling program to unix
    By dicdic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2014, 07:47 PM
  2. Regarding to run a package program in java
    By prasadk8910 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2013, 06:09 AM
  3. Replies: 21
    Last Post: June 12th, 2013, 11:33 AM
  4. Rational program calling up from different file but in same package
    By my21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2013, 12:00 PM
  5. Simple package problem, package does not exist error
    By Farmer in forum Object Oriented Programming
    Replies: 3
    Last Post: August 23rd, 2011, 11:18 AM