Win a copy of Java by Comparison (eBook) this week in the Java in General forum!

Omar Jouda

+ Follow
since Jul 11, 2012
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Omar Jouda

Campbell Ritchie wrote:Confusion: command line: a place where you write instructions to the operating system. Terminal: a place where you write instructions to the operating system. You usually call it command line on Windows and terminal on *nix. The *nix terminals are much more powerful than the command line. A console (small c) is a place where you write instructions to the operating system. A Console (large C) is a class in the package, which can take instructions from the console. Beware: if you start your app in certain fashions (I think, including by running it from an IDE), you get null instead of a Console object, and you cannot use the Console class’ methods.

What the Deitel book means is that you are supposed to start the app like this at a console
java HolidaySked 359
There is a window in Eclipse called console, which shows the input and output, but it behaves rather like a combination of System.out and System.err, not as a proper console. You can enter input to the running program, but not to the OS. This shows the hazards of using IDEs when you are first learning. You get confused because things like command‑line arguments are hidden. Stop using Eclipse.
If you insist on using Eclipse, I think what you need to do is:-

  • 1: R-click the name of your class and get a dropdown list.
  • 2: Click run configurations.
  • 3: Open the arguments tab.
  • 4: put 359 in the command line arguments box (not the JVM options box).
  • Not certain; I would have to run Eclipse to check that.

    Wow, I never knew we can pass arguments in Eclipse. This made it much more easier to test cmd line arguments while studying.
    I value all your experienced advice greatly, but just focusing on CMD makes it very tedious, hard, and time consuming. I mean I try to practice something, but I get errors which I don't understand why they come up and why they come up. It gets very frustrating, I can't practice any theory I am focusing on. I try something , another completely different weird error comes up.
    Like now, I tried opening CMD to practice Command Line arguments , I compiled the .java file. I then try to run it using java ,but it gives me an error that the class was not found. I mean it is right there but the CMD doesn't want to execute it.
    I created a post before in the OCAJP7 forum about this. I struggled for hours with it despite Mr. Roel's kind efforts with me. I still did not successfully execute programs or packaged programs using command lines. I am just relying on my theoretical knowledge from books but I can't practice CMD without getting frustrated. Very Hard times.
    4 years ago

    Campbell Ritchie wrote:This version has no input validation, and does everything in the main method, which is usually not a good idea. It might be better to pass args to the constructor of a class which extracts the values from the array.

    This is a very smart way to approach it indeed. It gets a bit confusing for with loops because I kind of have to visualize it clearly in my head to understand.
    You mentioned that you can pass the args to a constructor of a class. Methods can't call constructors the way they call other methods right? So, do you mean invoke a constructor of a class you create using new Classname(args); ?
    4 years ago
    It is starting to get a bit confusing.
    I never paid much attention to cmd lines but they turned out to be much more complicated for different situations. I just encountered the subject , which is why I am unable to know the problem. Especially, like you said, the IDE does everything with just one click of a hotkey and it does all the work. Not a good habit to start programming.
    I will try to debug the compiler errors myself and review the CMD lines from other books because I have really taken much of your time.
    I really appreciate your efforts to helping a complete beginner like me! and I learnt a lot these few hours from your quick and straight forward replies. I would like to thank you for that!
    It is time for me to do abt more research and trial and errors with the CMD. Oh boy, its gonna be a long day haha. No more Ctrl + F11 on Eclipse.

    Roel De Nijs wrote:It's a long time ago since I used the command line myself for compiling and running java files. So, it's all a bit rusty

    This should do the trick: javac E:\Development\workspace\BackToJava\src\com\ocaexam\tutorial\

    An alternative could be:
    cd E:\Development\workspace\BackToJava\src
    javac com\ocaexam\tutorial\
    (and if you want to compile all java files in a given package then you can do: javac com\ocaexam\tutorial\*.java)

    Yup , that did the trick. But the outcome.. hahaha. How come it worked without using classpath or -cp??

    Roel De Nijs wrote:When your class is not in a package, you can compile it with javac -cp Development\workspace\BackToJava\src

    When your class is in package com.ocaexam.tutorial, you can compile it with javac -cp Development\workspace\BackToJava\src com/ocaexam/tutorial/

    In both cases it's not needed to change the current directory to the source directory as you can provide it with the -cp option.

    All forward or backward slashes? Either way, it still doesnt work.
    I think I am missing something here. Does the current directory of the cmd affect the results? I thought -cp allows you to compile any class from any directory.
    Ok, -classpath only includes the directory for the source but without the package. I understand if it is not in a package.
    If it is, like in this case in package com.ocaexam.tutoria . The result from your code is :-

    Roel De Nijs wrote:You forget about the package of your java file. The directories which are part of the package should not be part of the classpath, only the source-directory (and eventually directories with already compiled classes) should be.

    So make the src directory the current directory (cd E:\Development\workspace\BackToJava\). Then you can compile your file with javac com/ocaexam/tutorial/ The IDE is doing that all for you, so now you can see what a lot of work it's doing for you

    To get this clear.
    If the java file is not in a package i.e in the source directly.
    To compile while CMD directory is E:\ ( javac -cp Development\workspace\BackToJava\ ) Correct?

    If the java file is in a package like this case.
    Step 1: I have to put the cmd directory to source directory by ( cd E:\Development\workspace\BackToJava\ )
    Step 2: ??

    Roel De Nijs wrote:

    Omar Jouda wrote:I just keep getting source file not found when trying to compile source code in a package on a different drive.

    Could it be a network security issue? Or maybe you saved the file with a text editor and the text editor added ".txt" extension for you, so now the file is called instead of

    I dont really think it is a network security issue. Because when I type javac -version, it gives me the latest.
    It is a ready created .java file from an IDE. I am trying to compile it by the CMD. So I am sure it is saved as .java extension.
    It is not a compile error. I just can't get it even to compile. Not sure what could be the problem.

    Roel De Nijs wrote:That's true! But when I studied Java (10 years ago) I had to use Notepad, simply because our family pc was not capable of running an IDE. And you are right: it's frustrating to have compiler errors one after another when trying to make an application. An IDE is a lot easier, because it spots for you the compiler errors. But with a simple text editor you'll become more careful, just because a little mistake results in compilation error (and having to change source code, compiling again,...). So you'll be more aware of the mistakes you have made already and prevent you to make these mistakes again. And because you also don't have code completion in your text editor, you'll learn the Java API more quickly.
    On the certification exam you don't have a compiler and you'll get a lot of code snippets which you have to study and decide if they compile, and if they do what will be the output. So there is no IDE to do that job for you. That's why you should consider preparing for the certification exam without an IDE.

    There could not be a more perfect answer.
    It was a big problem for me when I did some mock questions last year. Much harder, so I would keep copy/paste the question code and put it in the IDE. I would assess the result and then figure out why it happened. I kept doing that for several questions until I got the hang of it.
    Practicing with notepad from the start would make you a much more experienced programmer.

    And you are right: it's frustrating to have compiler errors one after another

    The frustration is starting to kick in already haha. I just keep getting source file not found when trying to compile source code in a package on a different drive.

    Roel De Nijs wrote:

    Omar Jouda wrote:I am just studying them now, as I have been used to the IDEs all the time.

    If you are studying for the exam, you should do this definitely without an IDE. Just use NotePad or NotePad++ or the text editor of your choice and just use javac/java to compile/run your little programs.

    I agree, however, the IDEs helps me spot compiler errors when I am focusing on certain subjects. Using Notepadd and CMD would start to be very frustrating after a very short because I would get this whole bunch of errors.
    I am also trying to cope with the -d and -cp commands. What is the function of the java -d not javac -d. Does it like allow you to change the parameters of the class without recompiling or?

    Roel De Nijs wrote:It all depends on what you are planning to do in the future. If you want to become a java developer this exam is really a must-have (just like the OCPJP 7). If you prepare yourself seriously (and not just to pass the exam), you will (as you already described in your 1st post) learn a whole bunch about the java programming language. And that will definitely benefit the quality of the code you write.

    Frankly, my aim is not to become a java developer. However, I want to start creating applications, programming electronic devices etc...The OCAJP is completely theoretical so I don't know if attaining the certificate would help as much. I will be going to university this year and I am planning to work somewhere in engineering or programming.
    I am not even know if Java is the best start, but I already did.

    Lalit Mehra wrote:Hi Omar,

    It does worth it to be certified ... It feels great.
    But above all you need to ask yourself that at what extent do you wish to pursue it, i mean will you build up on it or just leave the coarse after the certification.
    Certifications are a good way to attain knowledge, and be somewhat more noticeable to the employers also.

    Well. I agree certificates are great.
    Basically, I am an engineering student which would be my career, but programming knowledge is required. So I would rather expose myself to more languages than strictly stick to java. Hence, there is a low chance that I would push on further. Hence, most probably I will leave the course, and try and build up on my practical knowledge instead, because currently I have none.

    Roel De Nijs wrote:But you are talking about command line. How are the commands to invoke java/javac different. As far as I know they are exactly the same. The only thing that is different, is the classpath separator: on windows it's a semicolon (;) and on Unix/Linux/Solaris it's a colon (.

    java -cp . com.example.MyAmazingClass will work on every operating system where java is installed.

    Oh yes, you are right. Also on the slashes, windows is backward and others are forward.
    Also, the line seperators differ from /r and /n. But I am not sure what it does exactly.
    I think these are the only differences haha. It was a stupid question.
    I guess I got overwhelmed with the java -d java -cp java -cp . and javac. I am just studying them now, as I have been used to the IDEs all the time.

    Roel De Nijs wrote:Because your constructor has invalid syntax. Take a close look to your constructor and see if you can spot the problem.

    The parenthesis for empty parameters?
    So it should be public Earth() { etc.. } .

    I wrote it exactly as in a book of mine. Apparently it is wrong.


    Roel De Nijs wrote:How are java and javac different?

    Oh, I meant java and javac lines are different for each operating system.