• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to compile and execute non-super-simple Java-classes via prompt

 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

When we want to execute a Java-programm in the prompt, I am able to do so, if it is very simple - let's say a typical 'Hello World!'-programm with only
once class with only one main-method inside.
I just use 'javac HelloWorld.java' -> in this way I get the ByteCode, i.e. a class-file and then I use 'java HelloWorld' to run it.

But if i want to do it with a programm where I refer inside the java-file (a class) to another class and there also indirectly to a method which was created in the other class - it does not work - to illustrate it better:

Code from the 2nd class which I do not want to execute in the end:




This class I can compile and get a Test2.class-file.

Here is the code of the class i would like to execute within the prompt, in the end:



The code does work in the IDE, but I cannot compile this class using 'javac Test.java', but now I get following error-message:
Test.java:14: error: cannot find symbol
       Test2 t2 = new Test2();
       ^
 symbol:   class Test2
 location: class Test
Test.java:14: error: cannot find symbol
       Test2 t2 = new Test2();
                      ^
 symbol:   class Test2
 location: class Test
2 errors


How can I make it possible to execute this little Test-class within the prompt?

Grateful for any input:)
 
Rancher
Posts: 4588
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you compiling from?
That is, which directory are you in?

Looks like you are in the testing directory.

Since your code is in a package you ought to be in the directory the root package is in, so one up from where you are currently running it.
Note you would need to do javac testing/Test.java instead.
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those the only error messages you get?
Did the problems start when you started naming the packages? I presume you know about packages; if not look in the Java™ Tutorials.
There are all sorts of details you haven't told us.For example, the directory structure and package names must match.
Let's guess. Let's assume you are keeping the source (XXX.java) and binary files (XXX.class) in the same locations. Let's guess that you have navigated to the parent directory where the contents of the current directory would include your package.ABC etc mean you might find other files there, but they aren't relevant to your current problem.OROROn a Mac/Unix/Linux you would use / to separate directories.
 
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another solution is to use the javac's -cp option to refer to where the testing folder is located.  If it is up one level, -cp .;..\. would refer to the folder up one level.  For Windows:
javac -cp .;..\. Test.java
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Another solution . . .

Let that remind us all how many different ways there often are to do something.
 
Michael Mutek
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, great and thanks to all of you guys - problem is solved and I learned something new.

Basically, when it would not be for a current assignment-problem I want to solve, I am guilty of actually never using CMP/shells, but just the conventient IDE to run programs.
So I thought that even when you have classes inside a package, you can just move right away to the directory where the classes are lying and execute the known cmd-commands - I had no clue that you always need to start from the root-folder which is the src-folder when typing in the CMD-commands.

Anyways, thanks again:)


update:


I have tried the recommendations in regard to myy actual task and the first step, i.e. compiling and creating class.-files have worked out - see attached image.

But when I want to execute the Jit-class-file, it does not work - see at 2nd attached image.
Does anyone has an idea why this might be the case here?

offtopic question (I hope that this is ok)  :
I am new to this forum and I wonder if within this community there is also a kind of marketplaces where once could book tuition sessions from java tutors?



cmd_compile.PNG
[Thumbnail for cmd_compile.PNG]
cmd2.PNG
[Thumbnail for cmd2.PNG]
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy'n'paste fro the command line rather than posting screenshots.
You can compile the code distantly by using the -cp xyz option as Norm showed you. With a little fiddling of the classpath, you can compile or execute it from a folder nowhere near your code.
 
Norm Radder
Rancher
Posts: 3795
35
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
 
Michael Mutek
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please copy'n'paste fro the command line rather than posting screenshots.
You can compile the code distantly by using the -cp xyz option as Norm showed you. With a little fiddling of the classpath, you can compile or execute it from a folder nowhere near your code.



I do not get how this '-cp...' command work and could not find something helpful with google, but your described method before should likewise work, as it
did for me in regard to the previous simple example (java testing.Test Hello).

@Norm Radder:
Thanks and al right, here are the copy/pasted contents of my CMD:

Microsoft Windows [Version 10.0.18363.900]
(c) 2019 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\User>cd Desktop\Studium-OTH\Lernplan\Semester-3\Java Advanced\ProjectAJP_2020\FinalProject\Jit\src

C:\Users\User\Desktop\Studium-OTH\Lernplan\Semester-3\Java Advanced\ProjectAJP_2020\FinalProject\Jit\src>javac main\java\de\oth\jit\*.java

C:\Users\User\Desktop\Studium-OTH\Lernplan\Semester-3\Java Advanced\ProjectAJP_2020\FinalProject\Jit\src>java main.java.de.oth.jit.Jit
Fehler: Hauptklasse main.java.de.oth.jit.Jit konnte nicht gefunden oder geladen werden
Ursache: java.lang.NoClassDefFoundError: de/oth/jit/Jit (wrong name: main/java/de/oth/jit/Jit)

C:\Users\User\Desktop\Studium-OTH\Lernplan\Semester-3\Java Advanced\ProjectAJP_2020\FinalProject\Jit\src>


edit:
For better readability I have just made the CMD-contents italic - I hope that is alright or is there a prefered option to share CMD-contents here?
 
Norm Radder
Rancher
Posts: 3795
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how this '-cp...' command work  


-cp is not a command,  it is an option for the javac (and other) commands.  Read the API doc for the javac command to see what it is used for.
https://docs.oracle.com/javase/8/docs/technotes/guides/javac/index.html

java main.java.de.oth.jit.Jit
Fehler: Hauptklasse main.java.de.oth.jit.Jit konnte nicht gefunden oder geladen werden
Ursache: java.lang.NoClassDefFoundError: de/oth/jit/Jit (wrong name: main/java/de/oth/jit/Jit)
java.lang.NoClassDefFoundError: de/oth/jit/Jit (wrong name: main/java/de/oth/jit/Jit)


The compiler has read the class file and found that the wrong path was used to select it.  The real package/class name is de/oth/jit/Jit not main/java/de/oth/jit/Jit)

Change directories to the folder that contains the de folder and use
java  de.oth.jit.Jit

I assume there is a jit.class file in the jit folder.
Note classnames should start with uppercase letters.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Mutek wrote:. . .  made the CMD-contents italic . . .

Good idea. But you can also put the text into code tags (probably with the text option) or quote tags. It will look even better if you write [quote=Campbell's Terminal]Blahblahblah[/quote] or similar.
 
Michael Mutek
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey^^

Yeah, the classname actually has been started with an Uppercase - classname: Jit.
By the way, that brings me to another question where people on stackoverflow seem to have different opinions:
What is the common convention for project-names? Starting Uppercase (as for classnames) or lowercase (as for packages).

Anyways, you have helped me a lot.
Makes sense now that the command in regard to the execution of a class-file needs to be done one class above where the package, in which the class does exist, is
and thus the execution command (java ........ ClassName arguments) start with the name of the package.

Worked out well now - thanks again^^

 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a universal project name convention. It would be agreed in the individual companies, etc.
 
Michael Mutek
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al right, thanks^^
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic