$ javac Zoo.java
$ java Zoo Bronx Zoo
The output is what you might expect:
Bronx
Zoo
It is frightful only at the very beginning. Then - you'll like it.
Jj Roberts wrote:Hello and welcome to the Ranch!
In Java 11 you can compile and run a single-file program by passing it directly to the java tool. That is what you are doing the second time.
It seems odd that it works that way, but says Zoo is the wrong name when you use javac and then java. What happens when you give it different arguments, like 'giraffe zebra'? What does your project's directory structure look like?
It is frightful only at the very beginning. Then - you'll like it.
Campbell Ritchie wrote:Welcome to the Ranch
![]()
I suggest you show us the code you are running please. “Wrong name” errors are often caused by having a package name in the code and putting the .class file in a folder with a name different from its package. As JJ says, you can run an uncompiled single XYZ.java file in Java11+; the fact that it isn't complaining suggests your .class files have a different name. Please copy'n'paste the text from your command line in future because it is much easier to handle like that. I can't see the screenshot from here, and had to open your post twice.
It is frightful only at the very beginning. Then - you'll like it.
Dave Tolls wrote:Which shows the esact issue, that Campbell mentioned.
Your Zoo class is in a package (com.cha.iru) which means its full class name is com.cha.iru.Zoo, and you would be expected to execute the compile file from the root directory for your package structure (ie the directory that contains the com folder). And the command would be:
java com.cha.iru.Zoo something something
It is frightful only at the very beginning. Then - you'll like it.
Dave Tolls wrote:Where are you executing the command from?
Where is the Zoo.class file?
What exact command are you executing?
It is frightful only at the very beginning. Then - you'll like it.
Junilu Lacar wrote:Try changing directory to ..\first_project\src then do what Dave said to do.
If that doesn't work, cd to ..\first_project\out -- check to see if that's where your .class files are after you compile. If you're compiling from the command line, your class files might be under the ..\src directory though.
It is frightful only at the very beginning. Then - you'll like it.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Say you are in the directory my/folder/ and your Java code has a package statement package foo.bar;, then the commands you want to issue from my/folder are
javac foo/bar/Zoo.java
java foo.bar.Zoo one two
The package statement translates into a folder path of foo/bar. javac take a path to a file. java takes a fully qualified class name.
It is frightful only at the very beginning. Then - you'll like it.
Iryna Chakraborty wrote:Using package it doesn't work. It works only when class is in the source folder directly. Why is it so?
All things are lawful, but not all things are profitable.
Jj Roberts wrote:It does work if you are running it from the correct directory. What Knute is saying is that if your folders looked like my/folder/foo/bar/Zoo.java you would have to run
from the 'folder' directory. You have to run the commands from the directory which contains your package. In your case your file is at com/cha/iru/Zoo.java. What you need to do is navigate to the folder which com is in. Go and do that. Once you are in com's parent folder, you will need to run
(of course, if you are on Windows, the slashes go the other way).
Not sure, but I think there are many programs, including javac, which can automatically detect slashes and change / to \ as required. I don't think that will work for escapes like \n however.Iryna Chakraborty wrote:. . . slashes don't play important role . . .
My pleasure.Iryna Chakraborty wrote:Thank you very much!
I'm glad you know more about Windows than meIryna Chakraborty wrote:PS Jj Roberts, slashes don't play important role, it works both ways. But thanks for a remark.
Jj Roberts wrote:I'm glad you know more about Windows than me
. My exposure to Windows has been very limited. More than happy to stay on Ubuntu
![]()
Jj Roberts wrote:I'm glad you know more about Windows than me
. My exposure to Windows has been very limited. More than happy to stay on Ubuntu
![]()