Results 1 to 17 of 17
Thread: Compiling problems
- 01-04-2011, 01:25 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:
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).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.
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.
- 01-04-2011, 06:37 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Can you post me some source that isn't working?
- 01-04-2011, 06:51 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
I guess the problem is with your classpath that you are referring to
- 01-04-2011, 02:43 PM #4
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.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-04-2011, 04:22 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 01-04-2011, 08:06 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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:
would I also have to compile with:Java Code:java -cp file/path filename
but Im not quite sure.Java Code:javac -classpath file/path filename.
- 01-05-2011, 04:42 AM #7
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,
This will create the filename.class file in the same directory. Confirm that and thenJava Code:javac filename.java
Similar process for c:\java code\chapter2Java Code:java filename
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-05-2011, 08:39 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-05-2011, 08:42 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-05-2011, 09:51 AM #10
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!
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-05-2011, 09:56 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-05-2011, 09:56 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-05-2011, 10:10 AM #13
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?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-05-2011, 10:39 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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 ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-05-2011, 11:10 AM #15
Thanks a lot for the clarification. :D
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-05-2011, 11:18 AM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
- 01-05-2011, 08:49 PM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
-
how to get compiling
By philgrek in forum New To JavaReplies: 1Last Post: 05-25-2010, 11:53 PM -
Problems compiling
By Karnij in forum New To JavaReplies: 6Last Post: 06-29-2009, 04:25 AM -
compiling problems.
By Tikasas in forum New To JavaReplies: 7Last Post: 04-13-2009, 10:00 PM -
JAVA ME - Problems compiling and running in Eclipse
By Nicsoft in forum EclipseReplies: 2Last Post: 03-20-2009, 10:58 AM -
problems when compiling
By valery in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks