• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Running code from the command line

 
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, coderanchers! I'm trying to run the code from cmd.
In the book "OCA Study Guide" there's an explanation how to run it:

$ javac Zoo.java
$ java Zoo Bronx Zoo

The output is what you might expect:
Bronx
Zoo


In my case I have this:

Why it goes on without errors by adding ".java" for the second time?
show.jpg
[Thumbnail for show.jpg]
 
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?



I'm glad to be here Thanks. Giving other parameters changed nothing. Here's a screen:
cmd_3.jpg
[Thumbnail for cmd_3.jpg]
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



Thank you, Campbell

This is my code and screen of the project structure:
project_structure.jpg
[Thumbnail for project_structure.jpg]
 
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Adding full path to class didn't give any changes
 
Dave Tolls
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you executing the command from?

Where is the Zoo.class file?

What exact command are you executing?
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Where are you executing the command from?

Where is the Zoo.class file?

What exact command are you executing?



1. I'm executing from the Command Prompt
2. the Zoo.class file is in the package 'com.cha.iru'
3. first one - javac Zoo.java
   second one - java com.cha.iru.Zoo

Error is the same:

Error: Could not find or load main class com.cha.iru.Zoo
Caused by: java.lang.ClassNotFoundException: com.cha.iru.Zoo
 
Sheriff
Posts: 14750
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



Thank you, Junilu Lacar!

It works after putting class just in source folder without a package as in the book's example:

javac Zoo.java
java Zoo fox fox2


output:

fox
fox2


Can you explain me why is it so? I don't understand why it was working in cmd as "java Zoo.java" using package and without it it works as in the example from the book.
 
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


Using package it doesn't work. It works only when class is in the source folder directly. Why is it so?
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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). Don't forget to leave theat the top of your file. Now try it.
 
Knute Snortum
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Iryna Chakraborty wrote:Using package it doesn't work. It works only when class is in the source folder directly. Why is it so?


If you build and call the class correctly, it will work.  Jj Roberts gave you some good examples to follow.  Try them and get back to us.
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jj Roberts, Knute Snortum,

Thank you very much! Now everything is working perfectly well.

The problem was that I tried to run it not from the source folder (src) but being inside it Now I know how to run it correctly.

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).



PS Jj Roberts, slashes don't play important role, it works both ways. But thanks for a remark.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Iryna Chakraborty wrote:. . . slashes don't play important role . . .

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.
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Iryna Chakraborty wrote:Thank you very much!

My pleasure.

Iryna Chakraborty wrote:PS Jj Roberts, slashes don't play important role, it works both ways. But thanks for a remark.

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  
 
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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  



On the other hand I've been developing Java on Windows for about 15 years and I didn't know that either.
 
Iryna Chakraborty
Greenhorn
Posts: 13
IntelliJ IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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  


I can say the same thing about my knowledge of Ubuntu and others from Linux.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!