File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Associate Certification (OCAJP 7) and the fly likes Which CMD lines to know? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Which CMD lines to know?" Watch "Which CMD lines to know?" New topic
Author

Which CMD lines to know?

Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Do we have to know the CMD lines for different operating systems?
For example javac and java command lines are different. I only know for windows, do we have to know other operating systems too?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

How are java and javac different?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Roel De Nijs wrote:How are java and javac different?

Oh, I meant java and javac lines are different for each operating system.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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.
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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.
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Omar Jouda wrote: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.

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.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Omar Jouda wrote: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?

Have a look here for a simple example of the -D option.
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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 MyClass.java.txt instead of MyClass.java?
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
It is not a compile error. I just can't get it even to compile. Not sure what could be the problem.


[Thumbnail for 1.jpg]

Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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 MyClass.java.txt instead of MyClass.java?

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.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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/GreetingsUniverse.java. The IDE is doing that all for you, so now you can see what a lot of work it's doing for you
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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/GreetingsUniverse.java. 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\ GreetingsUniverse.java ) 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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

When your class is not in a package, you can compile it with javac -cp Development\workspace\BackToJava\src GreetingsUniverse.java

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

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.
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Roel De Nijs wrote:When your class is not in a package, you can compile it with javac -cp Development\workspace\BackToJava\src GreetingsUniverse.java

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

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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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\GreetingsUniverse.java

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

Joined: Jul 19, 2004
Posts: 5126
    
  12

Another alternative:
cd E:\Development\workspace\BackToJava\src\com\ocaexam\tutorial\
javac GreetingsUniverse.java
(or javac *.java)

So when compiling packages doesn't matter, but when running a java application they definitely do matter!
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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\GreetingsUniverse.java

An alternative could be:
cd E:\Development\workspace\BackToJava\src
javac com\ocaexam\tutorial\GreetingsUniverse.java
(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??



[Thumbnail for 2.jpg]

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Omar Jouda wrote:How come it worked without using classpath or -cp??

My java file didn't contained any code, just an empty class declaration. That's why it worked.

For you to make it work you'll have to do something like:
cd E:\Development\workspace\BackToJava\src
javac com\ocaexam\tutorial\planets\*.java
javac -cp . com\ocaexam\tutorial\GreetingsUniverse.java
java com.ocaexam.tutorial.GreetingsUniverse
Omar Jouda
Greenhorn

Joined: Jul 11, 2012
Posts: 19
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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Omar Jouda wrote:It is starting to get a bit confusing.

Yes, using an IDE it seems all very easy. But if you have to do it yourself for the very 1st time it can get quite confusing rapidly. But no worries if you have compiled 100s of programs it will be a piece of cake in the end

Omar Jouda wrote: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!

You are welcome! We all started programming with Java on some day and were (and still are) very happy and pleased to get a reply on our questions/doubts. That's the big strength of a community like JavaRanch!

Omar Jouda wrote:It is time for me to do abt more research and trial and errors with the CMD.

If you should have other problems/questions, I would suggest using the Beginning Java forum. It's the perfect forum for your questions (because you are beginning to learn java) and it also has more traffic than this forum, so more people who can help you with your doubts, questions,... and provide you with tips/hints to understand things a bit better.

Good luck and happy learning! Java is a great language to learn to develop applications (but that's of course a bit biased )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which CMD lines to know?
 
Similar Threads
Systems level difference between osx linux and unix
can we write operating system using JAVA
using 1.5 classes in 1.4?
java virtual machine
PrintWriter constructor I/O exception