Results 1 to 17 of 17
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Compiling problems

    Hi, this may be a basic question, but I am not sure what is wrong.

    I am very new to Java, I have a little bit of C++ and racket experience, I just picked up the book Thinking in Java 4th ed.

    I finished the first chapter, and completed all the exercises fine, they all compiled correctly, and ran when I issued java Programname.

    As I was reading the book it said to download the source files from the authors website, mindview.net. I followed the installation directions there and now none of my previously working code runs.

    The changes I made were as follows(explained from the books site):
    1. added CLASSPATH to the system variables, with the full path to the newly downloaded code from the website.

    After this it stopped working and I received the following error:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: random
    Caused by: java.lang.ClassNotFoundException: random
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: random.  Program will exit.
    My thought was that when I added the CLASSPATH I was no longer able to use classes from anywhere but that path(that may be entirely incorrect, please correct me if it is).

    So I added the path to my directory I have the exercises stored in, which still didn't work correctly.

    Finally, I deleted the CLASSPATH variable, and it still didn't produce the desired result.

    I am a little unfamiliar with manual compilation in cmd, with C++ I used codeblocks and with racket I used drracket, I am trying to get more familiar with manual compiling.

  2. #2
    MediumDave is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Can you post me some source that isn't working?

  3. #3
    dharanikrm is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    I guess the problem is with your classpath that you are referring to

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Go to Start -> Run -> type cmd

    Then, type java and hit enter.

    If it gives you the usage details for java commands, then you have set the java class path correctly.

    If it says, 'java' is not recognized as an internal or external command; then you need to set your class path correctly.

    Give it a try and let us know.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Except, as is clear from the OP, that is not their problem at all.
    Java is clearly working, or they wouldn't be getting that error.

    To the OP, I really don't like the idea of setting the CLASSPATH in the system variables. It's not needed, and can lead to bad habits.

    So, let's assume that that CLASSPATH hasn't been set. What command have you run, in what directory?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I managed to get a somewhat working fix, but I still run into problems. added a few things to the classpath to get it working.

    I added the location of the book source code, which for me is located at:
    C:\TIJ4\code.
    I also added a location where I am storing my self made classes, that path is:
    c:\java code.

    To make less clutter I want to store the problems by chapter as I complete them, when I store them in folders it no longer lets them be run.

    I added:
    c:\java code\chapter1 exercises
    and I can run and compile stuff in the chapter 1 folder, but I cant get other folders to work.

    I tried adding c:\java code\chapter2
    and all the files in that folder can't be run.

    You mentioned not setting class path, if I delete all the information and the classpath variable how can it know where to run stuff?

    Im guessing I use something like:
    Java Code:
    java -cp  file/path  filename
    would I also have to compile with:
    Java Code:
    javac -classpath file/path filename.
    but Im not quite sure.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Make sure that your environment variable CLASSPATH (or PATH) is pointing to the bin directory of your java installation i.e. something like, C:\Program Files\Java\jdk1.5.0_12\bin

    Plus, have a separate variable JAVA_HOME, which should be set to C:\Program Files\Java\jdk1.5.0_12

    Once you have set this properly, you can directly go to the directory where you have your java files and run them right away. No need to mention your classpath every time on command prompt.

    You can go to c:\java code\chapter1 exercises and say,

    Java Code:
    javac filename.java
    This will create the filename.class file in the same directory. Confirm that and then
    Java Code:
    java filename
    Similar process for c:\java code\chapter2

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by sunde887 View Post
    Im guessing I use something like:
    Java Code:
    java -cp  file/path  filename
    would I also have to compile with:
    Java Code:
    javac -classpath file/path filename.
    but Im not quite sure.
    That's the idea.
    You use -cp to tell the compiler or the java runtime (it works with both) which folders you want it to look in for dependent classes.

    You really need to read this for javac, and this for java.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by goldest View Post
    Make sure that your environment variable CLASSPATH (or PATH) is pointing to the bin directory of your java installation i.e. something like, C:\Program Files\Java\jdk1.5.0_12\bin

    Plus, have a separate variable JAVA_HOME, which should be set to C:\Program Files\Java\jdk1.5.0_12

    Once you have set this properly, you can directly go to the directory where you have your java files and run them right away. No need to mention your classpath every time on command prompt.

    You can go to c:\java code\chapter1 exercises and say,

    Java Code:
    javac filename.java
    This will create the filename.class file in the same directory. Confirm that and then
    Java Code:
    java filename
    Similar process for c:\java code\chapter2

    Hope that helps,

    Goldest
    They've not got a problem with java or javac.
    You do not need to set the CLASSPATH. It is a bad idea.
    You should inform the compiler and the runtime about any classpaths it may need, whether this is simply locally declared in a batch file or in the command itself via -cp does not matter, but it should not be a global thing.

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Red face

    Quote Originally Posted by Tolls View Post
    You do not need to set the CLASSPATH. It is a bad idea.
    You should inform the compiler and the runtime about any classpaths it may need, whether this is simply locally declared in a batch file or in the command itself via -cp does not matter, but it should not be a global thing.
    The links provided by you above says that, if we mention -cp or -classpath with java or javac then the CLASSPATH environment variable will be overridden with the path provided in the command. Thats fine.

    If we don't provide -cp or -classpath, then the environment variable will be referred. In that case, why is it a bad idea to set the environment variable classpath?

    I mean, is there any advantage in not declaring the environment variable and providing path through command every time? :confused:

    Please advice!

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Because people get reliant on it and we get yet more questions here about why things don't work.

    The proper way to execute and/or compile a java program is to use its own classpaths, because you rarely if ever write a single project on a machine. I have, for example, a dozen projects on this machine, and that's not unusual for my clients. A Tomcat instance will have (usually) more than one site loaded onto it, especially in a test environment.

    CLASSPATH is a crutch, and results in people forgetting how to actually work with this software properly. It's second only to people sticking their classes into the ext directory under the runtime.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    Make sure that your environment variable CLASSPATH (or PATH) is pointing to the bin directory of your java installation i.e. something like, C:\Program Files\Java\jdk1.5.0_12\bin
    The CLASSPATH variable has nothing to do with finding native executable files. It's the PATH variable that should point to directories that contain those files. The CLASSPATH variable is a Java thingy and it should point to directories that contain compiled .class files as well as .jar files. Better not set this variable in the environment but set it on the command line (-cp or -classpath flag). Don't mix them up. b.t.w. the JAVE_HOME variable isn't needed either when one does just Java SE development.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Red face

    OOPS! My bad. :(

    The PATH (Not CLASSPATH) should be set to java bin directory.

    We must use the -classpath or -cp on command line to redirect compiler to the compiled .class files.

    If we are sitting in the same directory where the .class files are present, then we don't need to provide classpath, as the current directory will automatically be searched in that case.

    Did I get this correctly?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    If we are sitting in the same directory where the .class files are present, then we don't need to provide classpath, as the current directory will automatically be searched in that case.

    Did I get this correctly?
    Almost, on an MS Windows box the current directory is scanned for .class files; not so on a Unix/Linux box. It's always safer to use the "-cp ." flag when running classes from the current working directory.

    kind regards,

    Jos

    ps. I can't check it now because my Linux laptop is in the car right now and I'm too lazy to fetch it ;-)
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Thanks a lot for the clarification. :D

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by goldest View Post
    Go to Start -> Run -> type cmd

    Then, type java and hit enter.

    If it gives you the usage details for java commands, then you have set the java class path correctly.

    If it says, 'java' is not recognized as an internal or external command; then you need to set your class path correctly.

    Give it a try and let us know.

    Goldest
    Uhm, FYI, that has nothing to do with classpath.

    Edit: Nevermind, already said, but it can't be said often enough.

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, Im starting to understand you much better. Thank you very much for the help, Im just going to ask a few more questions. For learning is it safe to use classpaths and just remember how to specify manual places. If I delete everything in the CLASSPATH variable is it better to just cd to the directory with the class I want, then run it from there?

Similar Threads

  1. how to get compiling
    By philgrek in forum New To Java
    Replies: 1
    Last Post: 05-25-2010, 11:53 PM
  2. Problems compiling
    By Karnij in forum New To Java
    Replies: 6
    Last Post: 06-29-2009, 04:25 AM
  3. compiling problems.
    By Tikasas in forum New To Java
    Replies: 7
    Last Post: 04-13-2009, 10:00 PM
  4. Replies: 2
    Last Post: 03-20-2009, 10:58 AM
  5. problems when compiling
    By valery in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:35 PM

Posting Permissions

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