aspose file tools*
The moose likes Beginning Java and the fly likes New to Java  some questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New to Java  some questions" Watch "New to Java  some questions" New topic
Author

New to Java some questions

Chris none
Greenhorn

Joined: Feb 17, 2007
Posts: 5
Hello thank you for taking the time to read my first post. I previously have been in with C and decided to come over to Java for work experience. Seems everytime I make a jump from one to another some very simple basic questions that come up that never seems to be covered in any book and is expected that you just know, and when you don't its quite frustrating. Java, I've downloaded the latest version. 1.6.0 JDK or SDK whatever you want to call it. I bought a book Head first java and its quite amazing but there are little problems I am running into. Seems by the time the latest book is out and is trying to explain stuff to you the current release is already out dated, this book is explaining stuff in version 1.5.0 and I am assuming all this code will still work but I am having some problems. Ill try to explain. The compiler, I have created a dos command inside my Bin directory that opens me up there and lets me run the Javac command, problem is... the way the books explains it, the compiler doesnt seem to agree with and I cant figure out how to fix it. Sample code.



Simple program. Problem arises with the compiler saying that I must compile seperate java files for each public class I create. Why I dont know my book doesnt say I have to do that, and even when I break the code down and try that, I still get errors that doesnt let it compile.



Now I have no idea why the compiler is smart enough to tell me this but cant make the simple conversion that this file is complete in one and in the same directory and even when I break the files up it still doesnt compile. So as basic as this is, I am new to java and I have no reference to the compiler and all examples expect this to work. But now it doesnt and I have no clue what I am doing wrong or what is being done wrong. None of the begining tutorials on this site refer to any explanation or examples of actualy installing this SDK package, everything just says do this after its installed. Any recommendations would be quite appreciated. Thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

If HFJ doesn't say that public classes need to be in their own files, named after the class, than it should -- because they must be. We could talk about the "why" but I suspect you really don't care -- that's just the rule.

Let's get past that, shall we? What errors do you see if you do what the compiler says and put the public classes into separate, correctly named files?

By the way, when you compile, do this:

javac -cp . *.java

(that's "javac space dash cp space dot space star dot java). This tells the compiler to look in . (dot), the current directory, and to compile all the code it finds there. If you weren't doing something like this, then chances are that's the cause of your remaining errors.


[Jess in Action][AskingGoodQuestions]
Chris none
Greenhorn

Joined: Feb 17, 2007
Posts: 5
Ok I tried a few things...

that command you gave me, javac -cp, I couldnt get to work so I moved on.

I took the original classes in main file and removed them and made seperate files for each class. That allowed me to get past the compiler errors, but now I am not able to run the program.

I get this output...

Exception in thread "main" java.lang.NoSuchMethod.Error:main"

now what may be messed up?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Which class are you trying to start? The only one you can run as an application is the one with the "main()" method in it; looks like that's "GameLauncher", so you want:


java -cp . GameLauncher
Chris none
Greenhorn

Joined: Feb 17, 2007
Posts: 5
So in this case, I had 3 classes..

GuessGame.class
Player.class
GameLauncher.class


So I have to coompile each class on its own, javac GuessGame.java to get my .class file. Do this on all 3 classes.

Then use the command Java + whatever class contains the Main function to initialize the rest of the classes?

Am I understanding this correctly?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

notice in EFH's post that there is a dot "." in the command he gave you

java -cp . GameLauncher


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Chris none:

Am I understanding this correctly?


Yes, pretty much. But you don't have to compile each class separately -- you can do them all at once either by listing all the files on one command line or using a wildcard like "*.java".
mickey thukarl
Greenhorn

Joined: Feb 17, 2007
Posts: 1
[EJFH: Removed unnecessary quoted message ]

hi, this is mickey ,i have started java again after completing till jdbc,
this time i am looking forward to some good book like ""HORTAN's java""
but i don't think so if its available in the market...
how about you ?
i'd tried java(sun) tuorials but not satisfied //i need a simplified version to be effective for coding assistance ...
hey! are you using any special editor for this ??
ok
[ February 17, 2007: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by mickey thukarl:
[EJFH: Removed unnecessary quoted message ]

hi, this is mickey ,


Hi Mickey,

Welcome to JavaRanch!

Please start a new thread for your own questions, rather than intruding on an existing thread, as you've done here. Thanks.
Chris none
Greenhorn

Joined: Feb 17, 2007
Posts: 5
Originally posted by Ernest Friedman-Hill:


Hi Mickey,

Welcome to JavaRanch!

Please start a new thread for your own questions, rather than intruding on an existing thread, as you've done here. Thanks.


Yea Back Off My Thread buddy !!!

Just Joking, welcome to Java, hope you enjoy it. And to answer your question... yes Hortons does have a begining Java book, I almost bought it but decided to start with Head first Java, it takes a more visual approach to learning Versus Hortons reading and learning.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
I am a bit surprised nobody has told you to set up your classpath and path variables permanently. A perennial problem, which catches all beginners out. If you are getting compiler errors like "should be declared in a file named", your path variable is probably already set up correctly. I am a bit surprised you have path set up and not the simpler classpath.

You use the same technique to set up both variables, and as with most things on computers, there are several slightly different ways to do it. I told somebody else on this forum how to do it last week: try here, and look for my first post. Once you get to the "edit" bit you have to be very precise about what you do, and don't change the other parts of the environment variables.

All the best. CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to Java some questions