Results 1 to 17 of 17
  1. #1
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default super newbie, running from terminal

    I have been using NetBeans in Linux up until now without a problem, but now I want to run my app from the terminal. When I CD to the directory where my NamesLoop.class file is and type "java NamesLoop" I get all these errors and I am not sure how to fix this:

    Java Code:
    phil@phil-desktop-kubuntu710:~/NetBeansProjects/NamesLoop/build/classes/namesloop$ java NamesLoop
    Exception in thread "main" java.lang.NoClassDefFoundError: NamesLoop (wrong name: namesloop/NamesLoop)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

  2. #2
    jklsemicolon is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    Is it an applet?

    if not you could try typing "dir" and check if the file is actually in the directory you are in

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,270
    Rep Power
    25

    Default

    java NamesLoop
    Exception in thread "main" java.lang.NoClassDefFoundError: NamesLoop (wrong name: namesloop/NamesLoop)
    at java.lang.ClassLoader.defineClass1(Native Method)
    The message says the class file is in the package namesloop.
    You need to set the classpath to the beginning of the package and use the package.classname with java:
    java -cp <classpath-to_namesloop>;. namesloop.NamesLoop

  4. #4
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    OKAY, I tried the following and now it is totally confused:

    Java Code:
    java -cp /home/phil//NetBeansProjects/NamesLoop/build/classes/namesloop;. namesloop.NamesLoop

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,270
    Rep Power
    25

    Default

    totally confused:
    Explain please
    The classpath should stop before the package path. Your -cp looks like it includes the package path.
    Path to class file = classpath + package path

  6. #6
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Umm....Package path? Class Path? I have no idea what in the world you are talking about. I have never done this before. I don't know why this needs to be so complicated.

    Here is the program code:
    Java Code:
    package namesloop;
    
    /**
     *
     * @author phil
     */
    public class NamesLoop {
    
    String[] firstNames = { "Dennis", "Grace", "Bjarne", "James"};
    String[] lastNames = new String[firstNames.length];
    
    void printNames() {
        for (int i = 0; i < firstNames.length; i++)
            System.out.println(firstNames[i] + " " + lastNames[i]);
    }
        public static void main(String[] args) {
            // TODO code application logic here
            NamesLoop a = new NamesLoop();
            a.printNames();
            System.out.println("-----");
            a.lastNames[0] = "Ritchie";
            a.lastNames[1] = "Hopper";
            a.lastNames[2] = "Stroustrup";
            a.lastNames[3] = "Gosling";
             a.printNames();        
        }
    }
    The NamesLoop.class file is in the following path:

    /home/phil//NetBeansProjects/NamesLoop/build/classes/namesloop

    Therefore, the entire path including the filename would be:

    /home/phil//NetBeansProjects/NamesLoop/build/classes/namesloop/NamesLoop.class

    Based upon that information, can you give me the command I would type at the console prompt to get the program to run?

    I really appreciate your help with this. :)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,270
    Rep Power
    25

    Default

    Remove the package statement from your program and you will be able to enter: java NamesLoop as the command to execute your program.
    If you put your program in a class, then you need to understand packages and classpath.
    The sooner you understand classpath, the easier it will be.

  8. #8
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    I typed just java NamesLoop and here is what I received:

    Java Code:
    phil@phil-desktop-kubuntu710:~/NetBeansProjects/NamesLoop/build/classes/namesloop$ java NamesLoop
    Exception in thread "main" java.lang.NoClassDefFoundError: NamesLoop (wrong name: namesloop/NamesLoop)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
    Where can I read about classpath and why was this not setup correctly on my machine when the JRE/JDK or NetBeans was installed?

    If the classpath is "broken" on my machine, why can NetBeans run everything without a problem?

    Why did NetBeans add the "package" line if this keeps the programs from running via a command line prompt?

    Would it be helpful to refer to some ant script down inside the NetBeans directory to see how it runs the code?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,270
    Rep Power
    25

    Default

    Remove the package statement from your program
    Did you try what I suggested above?
    I don't like IDEs for beginners because of this problem. They do things that are beyond your knowledge.
    When you understand classpath and packages, you'll know how to execute your program. In the meantime you'll have to stay inside the IDE.

  10. #10
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Question Learning Java

    Hi everybody:

    I was wondering if someone could give me a little tip about the process of learning java. I have several doubts regarding how long would it take me to learn java? What could be the so called learning curve and how many hours a day is it necessary to spend studying and practicing in order to obtain a certain amount of knowledge that will enable me to "feel" like a Java programmer in some 2-3 years maximum to be a Jr Programmer at least, even without a college degree that is (I want to study Java very hard at the same time with my college education).
    When I learn something new, there's seems to be like a trillion other things that I need to know before I make it work. If I improved my knowledge at math could this make it easier??

    I would appreciate any comments or suggestions. I hope I'm clear enough.
    Last edited by Alexander Montero; 08-10-2008 at 01:21 AM.

  11. #11
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Did you try what I suggested above?
    I don't like IDEs for beginners because of this problem. They do things that are beyond your knowledge.
    When you understand classpath and packages, you'll know how to execute your program. In the meantime you'll have to stay inside the IDE.
    yes....sorry......I commented it out. This is how it appears now and it still does not work:

    Java Code:
    //package namesloop;
    
    /**
     *
     * @author phil
     */
    public class NamesLoop {
    
    String[] firstNames = { "Dennis", "Grace", "Bjarne", "James"};
    String[] lastNames = new String[firstNames.length];
    
    void printNames() {
        for (int i = 0; i < firstNames.length; i++)
            System.out.println(firstNames[i] + " " + lastNames[i]);
    }
        public static void main(String[] args) {
            // TODO code application logic here
            NamesLoop a = new NamesLoop();
            a.printNames();
            System.out.println("-----");
            a.lastNames[0] = "Ritchie";
            a.lastNames[1] = "Hopper";
            a.lastNames[2] = "Stroustrup";
            a.lastNames[3] = "Gosling";
             a.printNames();        
        }
    }
    Regarding IDEs, they are supposed to make your life easier, not break your code so it will not work from the command line. :(

    The only time I ever coded in notepad was when I was learning PHP and to this day I tell everyone it is the most useless language I have ever encountered, most likely because I was not using an IDE. Coding in PHP was like pulling my fingernails out using pliers.

    If I am creating any app in an IDE, running it outside the IDE should be a piece of cake. I worked in Visual Studio for years - I just ran the .exe file from the command line or double-clicked on the file and Voila! it would run.

    Now I am learning Java, creating console apps.....and there is no way to run them from the console? That seems VERY ironic. ;)

    My book said the .class file was bytecode and the JRE would know how to interpret and run it. I guess my textbook just does not know the subtle complexities of the real world and the mysterious classpath that keeps programs from executing.

    Fortunately, I have moved onto other chapters - that chapter was showing me how "easy" it was to pass parameters into my program from a command line. Oh.....the NamesLoop program I just chose at random to run - that was not the program for the chapter I was in, but if I can get SOMETHING to run then they will all run.

    After we get this mess sorted out, I want to run the programs on Windows machine and Solaris machines I have here. I can hardly wait for that excitement based upon how difficult this has been thus far.

    Again.....is there a web site that will tell me what I need to know so I can understand what is going on here?

  12. #12
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    OKAY - I have been looking at web sites that went on and on about classpath but gave no real examples on how to set this, or how to view the current setting in a Linux system.

    However, in fooling with the IDE and deleting the class file and re-building without the package statement in the code, it did spit out the following help.....FINALLY:

    To run this application from the command line without Ant, try:
    java -jar "/home/phil/NetBeansProjects/NamesLoop/dist/NamesLoop.jar"
    I don't know why it finally decided to help me, but that worked - and now that I have posted it here, I will always know where to find it. I did not even know I was creating jar files or what they are.....but for now I am just trying to get through this book.

    thank you :)

  13. #13
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Alexander Montero View Post
    I was wondering if someone could give me a little tip about the process of learning java. I have several doubts regarding how long would it take me to learn java?
    You might want to start a new thread rather than embedding your question within a detailed discussion of the classpath environment variable and how it is used to locate custom-created classes in your directory structure.

    How long it will take you to learn Java depends upon the quality of your teaching materials, the quality of the computer you are using, the amount of software development experience you have had in the past, the environment in which you will be studying (screaming kids and barking dogs), your concentration level, the path you will take (desktop apps, web apps, embedded apps), etc.

    In my case I am using a "Learn Java in 21 Days" book and want to be a Junior software developer by the middle of September. Well....maybe a junior/junior developer. ;) However, I have been creating applications for 25 years in BASIC, Pascal, C, COBOL, and VB.NET - Java is just a new syntax for me.

    Even for me, Java represents some new concepts - they talked about truncating attributes when casting between classes and that concept blew my mind.
    Last edited by pbpersson; 08-10-2008 at 04:39 AM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,270
    Rep Power
    25

    Default

    it still does not work:
    Can you show the console when you tried to execute the java program after removing the package statement?
    IDEs will make it easier as long as you keep using them. If you try to go outside on your own, you need to understand the environment and how to use the commands that were buried inside the IDE.
    Quick tutorial -
    The classpath contains the path(s) where the compiler and java program will search for class files and other resources. The classpath often contains jar files containing class files and folders with class files.
    The package path is the path represented by the package statement. The full path to a class in a package will be the classpath + the package path. For example a class file in package abc would be located in folder abc. To have java find that class file, you need a classpath that leads to the folder with the abc folder in it.

  15. #15
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks a lot for replying. those things you mention give me an idea as to what I'm up against.

    So, let's suppose you've had 0 experience in the past making any kind of applications like me, what would you do to learn java in 2-3 years maximum? perhaps the solution to my problem is keep studying and practicing at the same time and researching on all topics and the problems other programmers encounter that's gotta help somehow.
    Thanks again and good luck in your goal.

    Sorry about the confusion, I don't know how the site works.

  16. #16
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you show the console when you tried to execute the java program after removing the package statement?
    Java Code:
    phil@phil-desktop-kubuntu710:~/NetBeansProjects/NamesLoop/build/classes$ ls
    namesloop  NamesLoop.class
    phil@phil-desktop-kubuntu710:~/NetBeansProjects/NamesLoop/build/classes$ java NamesLoop.class
    Exception in thread "main" java.lang.NoClassDefFoundError: NamesLoop/class
    Caused by: java.lang.ClassNotFoundException: NamesLoop.class
            at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

  17. #17
    pbpersson's Avatar
    pbpersson is offline Member
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Alexander Montero View Post
    what would you do to learn java in 2-3 years maximum?
    Well.....um.....what do you mean by "learn Java"?

    I spent two years studying VB.NET and I was able to create desktop applications that could display things to the screen, open multiple windows, open files, create files, modify remote databases, display data in different ways......I could also write web applications that did all the same things.....but every time I did anything new I had to spend tons of time researching how to do it and much of my time was spent learning via trial and error. So after two years.....had I "learned" VB.NET or was I still just beginning?

    Quote Originally Posted by Alexander Montero View Post
    perhaps the solution to my problem is keep studying and practicing at the same time and researching on all topics and the problems other programmers encounter
    Go to the bookstore and get a "beginning Java" book and before you buy it, make sure the first chapter makes perfect sense to you - find something that is correct for where you are in your journey. That would be my advice. Looking at problems faced by programmers is not a good place to start because quite frankly it will all look like Greek to you and you could get discouraged.

    Once you have some knowledge so you can understand what they are talking about then it will be different. But you have to gain a little knowledge just to start out.

Similar Threads

  1. VT6530 Terminal Emulator 0.2.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-01-2008, 10:18 PM
  2. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:22 PM
  3. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 06:37 PM
  4. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:52 AM
  5. Problem when displays in the terminal
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:14 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
  •