• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Stuck for 10 Days: cannot Find Symbol Error

 
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I've been learning Java for a month now and have spent the last ten days stuck on this error. I have dug through hours of YouTube tutorials on Java datatypes, methods and classes; read countless forum posts from others with similar error messages; and have hammered my way through Oracle documentation and blogs...I'm spinning my wheels here! I was trying to see how far I could make it without having to post a question but I feel that 10 days is enough. Please lend a hand if you can.

The compiler seems to have an issue with my Flashcard class. The three files I've uploaded are the java files of my program. Since the limit for this post is three attachments, I'll paste the error:

symbol: class Flashcard
location: class MainProgram
MainProgram.java:12: error cannot find symbol
ArrayList<Flashcard> deckEntire = new ArrayList<Flashcard>();

symbol: class Flashcard
location: class MainProgram
MainProgram.java:15: error cannot find symbol
Flashcard FC = new Flashcard();

The arrow in both error messages is pointing to the word Flashcard.

The three files that make up my code are:

MainProgram.java
Self-explanatory
Takes user input (keyboard) and calls the showFront method from the Flashcard.java class. The number it takes corresponds to the flashcard number that will be displayed by the showFront method.

Flashcard.java
Flashcard class. Has a method showFront that takes user input (keyboard) and feeds it into a switch statement to display text to the console

GUI.java
Just playing around with Java's Swing library here. Nothing special.



Flashcard.java
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that MainProgram lies under a package called "flashcards"
Which package does class Flashcard lie under ?
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't read through your entire source code but I did notice one odd thing in your main program. It is under the For loop that creates a new object called FC and then adds the same object over and over again in the For loop.

I do not believe that your intention is to create FC over and over again, but a different object each time is that correct? If that is the case, I advise that you revisit and revise that portion of your code.
 
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class MainProgram and Flashcard are not in the same package and you're creating an instance of Flashcard without importing, plus class Flashcard is on the default package and you can't import from the default package.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I see that MainProgram lies under a package called "flashcards"
Which package does class Flashcard lie under ?



You're right, thanks for pointing that out. I changed the package name to match for all three files.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ansamana Sankarray wrote:Class MainProgram and Flashcard are not in the same package and you're creating an instance of Flashcard without importing, plus class Flashcard is on the default package and you can't import from the default package.



I updated the three files so the package name matches - two had a lower case F. Just to be clear, now that they're all under the same package there will be no importing of anything required, right?
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naziru Gelajo wrote:I haven't read through your entire source code but I did notice one odd thing in your main program. It is under the For loop that creates a new object called FC and then adds the same object over and over again in the For loop.

I do not believe that your intention is to create FC over and over again, but a different object each time is that correct? If that is the case, I advise that you revisit and revise that portion of your code.



My goal is to fill an ArrayList with objects of the Flashcard type (created in my Flashcard.java file). I had thought that I needed to create objects with a unique name for every iteration (eg. FC0, FC1, FC2, etc) but I saw a post mentioning that the FC object is destroyed after each iteration, so I needed to shove it into an ArrayList before the next iteration. As a result, I have six objects of type Flashcard in my ArrayList, but no variable name for which to identify / reference them. Therefore, if I want to reference one in particular, I have to use the built-in ArrayList method "get" in order to do so:



For my particular project I don't believe it's important to actually label each object in the ArrayList is there? When I say label, I mean declare each one with a variable name that uniquely identifies it (because I can just call deckEntire.get(int) right?
 
Ansu Sank
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:

Ansamana Sankarray wrote:Class MainProgram and Flashcard are not in the same package and you're creating an instance of Flashcard without importing, plus class Flashcard is on the default package and you can't import from the default package.



I updated the three files so the package name matches - two had a lower case F. Just to be clear, now that they're all under the same package there will be no importing of anything required, right?



Yes, no importing is needed if they are in the same package.
I'm new in Java too, I know the feeling of getting stucked
Hope your program works now
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:For my particular project I don't believe it's important to actually label each object in the ArrayList is there? When I say label, I mean declare each one with a variable name that uniquely identifies it (because I can just call deckEntire.get(int) right?



Yes, that's correct. And not just for your project, but for any Java program whatsoever. You can only access an object if something in your program has a reference to that object, but that certainly isn't limited to the code you wrote. Yes, the ArrayList is a list of references to objects, and you can get an object out of the ArrayList in exactly the way you said.

So the loop you wrote at the beginning of your code to put 7 objects into the list is just fine. However you could write it like this:



and as you can see, you don't even need a variable in your code at all. The new Flashcard object gets added straight into the ArrayList, which then holds a reference to it.

(I'm not suggesting that you ought to do that, your code is fine the way it is.)
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:I had thought that I needed to create objects with a unique name for every iteration (eg. FC0, FC1, FC2, etc) but I saw a post mentioning that the FC object is destroyed after each iteration...



Actually, FC is not an object in that loop. It's a variable name. The variable FC can contain a reference to an object of type Flashcard, but it is not an object itself. It's important to pay attention to the distinction because it's the idea of an object reference which gives Java a lot of its power.

So after each iteration, it isn't true that "the FC object is destroyed". What IS true is that the FC variable goes out of scope -- that means it can't be used outside of the loop.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Ben Hutchenson wrote:I had thought that I needed to create objects with a unique name for every iteration (eg. FC0, FC1, FC2, etc) but I saw a post mentioning that the FC object is destroyed after each iteration...



Actually, FC is not an object in that loop. It's a variable name. The variable FC can contain a reference to an object of type Flashcard, but it is not an object itself. It's important to pay attention to the distinction because it's the idea of an object reference which gives Java a lot of its power.

So after each iteration, it isn't true that "the FC object is destroyed". What IS true is that the FC variable goes out of scope -- that means it can't be used outside of the loop.



Thanks again for your comments, Paul. So now that I've got the package name consistent across all three files, I still see that the compiler has an issue with the "reference?" Flashcard. See attached screenshot. Can you advise on where I should be focusing?
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wonderful, it didn't attach. User-error.
error.JPG
[Thumbnail for error.JPG]
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look at the beginning of your code where you (hopefully) have a list of "import" statements. One of them needs to import Flashcard from the package which it's in.

But wait... earlier you said

I updated the three files so the package name matches - two had a lower case F. Just to be clear, now that they're all under the same package there will be no importing of anything required, right?



Since the three classes are in a package, their source code should be in a directory whose name is the name of their package. And they should have been compiled so that their compiled versions are in that same directory (or at least a directory of the same name).
 
Marshal
Posts: 80616
468
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you got it working yet?
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You need to look at the beginning of your code where you (hopefully) have a list of "import" statements. One of them needs to import Flashcard from the package which it's in.

But wait... earlier you said

I updated the three files so the package name matches - two had a lower case F. Just to be clear, now that they're all under the same package there will be no importing of anything required, right?



Since the three classes are in a package, their source code should be in a directory whose name is the name of their package. And they should have been compiled so that their compiled versions are in that same directory (or at least a directory of the same name).



Good morning Paul - here attached is my program directory - the folder is called Flashcards, so the directory name is the same as the package. I successfully compiled GUI.java and Flashcard.java into the same folder. Also, the import statements of my main program.
info.jpg
[Thumbnail for info.jpg]
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Have you got it working yet?



Hi Campbell, thanks! Glad to be here. Not yet; still fighting with the symbol error. Just heading into work - looking forward to more troubleshooting tonight!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the exact javac command you are using to compile this?
Include what directory you are executing it from.

Also, can you check whether you have declared a CLASSPATH in your environment variables...if you have hten that is probably the root cause of your issue.
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a classpath enviorement variable, you only need to update the path/PATH variable
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Can you post the exact javac command you are using to compile this?
Include what directory you are executing it from.

Also, can you check whether you have declared a CLASSPATH in your environment variables...if you have hten that is probably the root cause of your issue.



Hey Dave, I attached to this post my System Environment Variables.

Below is the command line output after having already successfully ran javac Flashcard.java and javac GUI.java from the Flashcards folder:

C:\Users\Don Verga Jr\Desktop\Flashcards>dir
Volume in drive C has no label.
Volume Serial Number is DC63-C805
Directory of C:\Users\Don Verga Jr\Desktop\Flashcards

02/15/2019  05:04 PM    <DIR>          .
02/15/2019  05:04 PM    <DIR>          ..
02/15/2019  05:02 PM             2,543 Flashcard.class
02/14/2019  06:57 PM             2,985 Flashcard.java
02/15/2019  05:04 PM               649 GUI$1.class
02/15/2019  05:04 PM               645 GUI$2.class
02/15/2019  05:04 PM               645 GUI$3.class
02/15/2019  05:04 PM               646 GUI$4.class
02/15/2019  05:04 PM               648 GUI$5.class
02/15/2019  05:04 PM               744 GUI$6.class
02/15/2019  05:04 PM             2,350 GUI.class
02/14/2019  07:00 PM             3,955 GUI.java
02/14/2019  06:53 PM             1,534 MainProgram.java
             11 File(s)         17,344 bytes
              2 Dir(s)  240,458,125,312 bytes free

C:\Users\Don Verga Jr\Desktop\Flashcards>javac MainProgram.java
MainProgram.java:11: error: cannot find symbol
               ArrayList<Flashcard> deckEntire = new ArrayList<Flashcard>();
                         ^
 symbol:   class Flashcard
 location: class MainProgram
MainProgram.java:11: error: cannot find symbol
               ArrayList<Flashcard> deckEntire = new ArrayList<Flashcard>();
                                                               ^
 symbol:   class Flashcard
 location: class MainProgram
MainProgram.java:14: error: cannot find symbol
                       Flashcard FC = new Flashcard();
                       ^
 symbol:   class Flashcard
 location: class MainProgram
MainProgram.java:14: error: cannot find symbol
                       Flashcard FC = new Flashcard();
                                          ^
 symbol:   class Flashcard
 location: class MainProgram
MainProgram.java:28: error: cannot find symbol
               new GUI();
                   ^
 symbol:   class GUI
 location: class MainProgram
5 errors
system-env-var.JPG
[Thumbnail for system-env-var.JPG]
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody suggested it is a CLASSPATH problem, and I am beginning to think that is correct. Please show us what happens if you write echo %CLASSPATH%
 
Marshal
Posts: 4794
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are two issues:

1. The package name and the associated directory/folder are named differently: Flashcards vs. flashcards
2. You should be compiling from one level up, not inside the package directory.

Either rename the Flashcards directory to flashcards and compile from the Desktop directory:
C:\Users\Don Verga Jr\Desktop>javac flashcards\MainProgram.java

or create a directory named flashcards (the package name) under Flashcards (the project name), and compile from the Flashcards directory:
C:\Users\Don Verga Jr\Desktop\Flashcards>javac flashcards\MainProgram.java
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Ron on those, I pretty much noticed the same.

@OP
Consider removing package statements until you more experienced. Having done that, you could keep simply all class files in the same directory and run from there.

Packages have use, but probably not in your case at this stage.

Once you done with this project, I advice you to take one step back, create few experimental classes and try to grasp packages concept on its own as well as how the complication and execution process goes having them defined. And practice that from console until you have a good understanding.


As a side note: I remember what mostly confused me about the packages - the difference between the file system directory and package. And the answer is - no difference. The difference comes from Java's point of view.

For instance, if you have defined class:

and your current working directory is:
/Users/ben/Documents/programming

in such case the compiled Apple class file supposed to reside in
/Users/ben/Documents/programming/bucket directory within the file system.

Note: the above wouldn't happen automatically without you explicitly specifying/making so.

So there are really 2 options (working manually from console):
1. create manually "bucket" directory and place compiled Apple.class file there
2. being in /Users/ben/Documents/programming directory, compile source file as such: "javac -d . Apple.java" (on Windows might slightly differ specifying current directory, but not sure about that), that would create package directory automatically

Now, how to execute such class after you compiled.

You navigate to: /Users/ben/Documents/programming
and execute instruction: java bucket.Apple

Now Java knows that need to look for package (=directory) "bucket" where Apple.class file supposed to be.

If you'd navigate to: /Users/ben/Documents/programming/bucket directory and would try issue same instruction, Java would look for Apple class file in "/Users/ben/Documents/programming/bucket/bucket",  and of course wouldn't find.

Things get a bit more complicated when you have several classes where each of those have different package declarations, then compilation and execution instructions are more complex, but let's keep this for your future.

I hope this information will serve you as a research starting point.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had created classfiles in my desktop\flashcards directory, so I deleted them all and am starting over.

Liutauras Vilda - I removed the package flashcards; statement from all three .java files. After getting this running I'm going to take a step back to hammer out all the holes in my knowledge / experience of package structure.

Following Ron's advice I renamed the folder to flashcards. So now all three .java files reference the package "flashcards", and the root folder name matches.

Campbell Ritchie - echoing %CLASSPATH% returns %CLASSPATH% so I'm assuming that shows it's not set.

C:\Users\Don Verga Jr\Desktop>javac -classpath "c:\users\don verga jr\desktop\flashcards"
error: no source files

C:\Users\Don Verga Jr\Desktop>javac flashcards\GUI.java

C:\Users\Don Verga Jr\Desktop>javac flashcards\Flashcard.java

C:\Users\Don Verga Jr\Desktop>javac flashcards\MainProgram.java
flashcards\MainProgram.java:11: error: cannot access Flashcard
               ArrayList<Flashcard> deckEntire = new ArrayList<Flashcard>();
                         ^
 bad class file: .\flashcards\Flashcard.class
   class file contains wrong class: Flashcard
   Please remove or make sure it appears in the correct subdirectory of the classpath.
flashcards\MainProgram.java:28: error: cannot access GUI
               new GUI();
                   ^
 bad class file: .\flashcards\GUI.class
   class file contains wrong class: GUI
   Please remove or make sure it appears in the correct subdirectory of the classpath.
2 errors

I have to go out for a few hours. Looks like success is on the horizon! Will be back soon, and thanks again to everyone...
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just realized the contradiction in my previous statement about having removed the reference to package flashcards from each java file, and then mentioning that they all point to the same package! To be clear, there's no reference to packages anymore.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:I think there are two issues:

1. The package name and the associated directory/folder are named differently: Flashcards vs. flashcards
2. You should be compiling from one level up, not inside the package directory.

Either rename the Flashcards directory to flashcards and compile from the Desktop directory:
C:\Users\Don Verga Jr\Desktop>javac flashcards\MainProgram.java

or create a directory named flashcards (the package name) under Flashcards (the project name), and compile from the Flashcards directory:
C:\Users\Don Verga Jr\Desktop\Flashcards>javac flashcards\MainProgram.java



There, I broke it all down to a more simplistic form:

Main.java


Flashcard.java:


Something is seriously lacking in my understanding of basic principles...
cmd.jpg
[Thumbnail for cmd.jpg]
 
Liutauras Vilda
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:There, I broke it all down to a more simplistic form


Have a cow for doing so. That was an excelent idea of yours. And engineering is really about that - about simplifying problems, wheher it is functionality, design or debugging problem.

Ben, you are close, I think you just got typo. When executing class after its compilation, you don’t need to add .java, just simply write: java ClassName (and path to it if you aren’t in that directory already).

So in your case that would be: java Test\Main
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Ben Hutchenson wrote:There, I broke it all down to a more simplistic form


Have a cow for doing so. That was an excelent idea of yours. And engineering is really about that - about simplifying problems, wheher it is functionality, design or debugging problem.

Ben, you are close, I think you just got typo. When executing class after its compilation, you don’t need to add .java, just simply write: java ClassName (and path to it if you aren’t in that directory already).

So in your case that would be: java Test\Main



You're right, it's about simplification. I broke it down to the basics (a file and a folder) and the plot thickens!

- Simplified to one file (test\main.java)
- No imports, no packages
- Set classpath; verified that classpath points to test folder; rebooted
- Consistent naming convention: lowercase filename matches lowercase class's name
- Successfully compiled main.java into main.class
- Noob Diaries, Chapter 1: Java contradicts itself by knowing the classpath but not finding said folder's class file
- Noob Diaries, Chapter 2: Aspiring programmer contradicts himself by not even knowing how to run a program even simpler than Hello World!

Next step - reinstalling java!
error.JPG
[Thumbnail for error.JPG]
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And thanks for the cow!
bart.jpg
[Thumbnail for bart.jpg]
 
Ron McLeod
Marshal
Posts: 4794
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:Consistent naming convention: lowercase filename matches lowercase class's name


Class names (and associated file names) should use CamelCase and start with a capital letter ... Main.java rather than main.java.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Ben Hutchenson wrote:Consistent naming convention: lowercase filename matches lowercase class's name


Class names (and associated file names) should use CamelCase and start with a capital letter ... Main.java rather than main.java.



Thanks for the tip Ron, I overlooked that.
- Deleted main.class
- Renamed the class declaration to MainProgram
- Renamed file to MainProgram.java
- Recompiled successfully

Pretty sure I'm not supposed to be running .java files (.class instead) but I entered java test\MainProgram.java to show the output - at least it confirms that it recognizes a .class file.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last post didn't attach
error2.JPG
[Thumbnail for error2.JPG]
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking this has to be environment-related. I'm certain that I set the classpath in the recommended way. Going back to learn more about the classpath and what java requires in terms of environment variables...
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hutchenson wrote:. . . . I'm certain that I set the classpath in the recommended way. . . . ...

There isn't a reommended way to set the CLASSPATH. You shouldn't set it at all. We asked to see your CLASSPATH last week; please show us what you have.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ben Hutchenson wrote:. . . . I'm certain that I set the classpath in the recommended way. . . . ...

There isn't a reommended way to set the CLASSPATH. You shouldn't set it at all. We asked to see your CLASSPATH last week; please show us what you have.



Hey Campbell, I showed it just before the Bart Simposon post above - c:\users\don verga jr\desktop\test. It's still set that way. I'll remove it and reboot and will try again.
 
Ben Hutchenson
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how relevant these variables are, but I had set them last week:

JAVA_HOME C:\Program Files\Java\jdk-11.0.1\bin
JRE_HOME C:\Program Files\Java\jre-1.8.0_201\bin

Screenshot attached showing ClassNotFound exception after attempting to run successful compiled program...
error3.JPG
[Thumbnail for error3.JPG]
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid screenshots; yours are unusual in being legible, but a lot of screenshots are illegible. Our link inludes a link to remind you how to copy from the Windows® command line.
I always thought the Windows® command line was case‑insensitive, but please try echo %CLASSPATH% with upper case CLASSPATH. If you are getting %classpath% back, either you haven't quite got the command correct, or there isn't a classpath at all.
Don't include the \bin part in JAVA_HOME.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your HOMEs shouldn't point to the bin, but to the installationfolder, you do have to include the \bin in your path though
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a package test; declaration in the MainProgram file (that isn't a good name for a class)?
If so, you need to execute it from the enclosing folder (here Desktop) with java test.MainProgram
Not a / nor a \ but a .
That is what the wrong name part means.

I don't like keeping ll my work in the desktop; I like to create a special folder, which you can do like this:-You only need to use line 1 once; then you use line 2 to go to your Java directory.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example that hopefully shows you how to make Java happy.

Create a directory, blah.

$ mkdir blah
$ cd blah

Now create the following source files in the blah directory:

Main.java

Foo.java:

Still in directory blah, compile and run:

$ javac Main.java
$ javac Foo.java
$ ls
Foo.class Foo.java Main.class   Main.java

$ java Main
Foobar!

What you've done here is you've created two classes, Main and Foo, in the "default" directory since there is no package declaration. That means that you have to run the java command from the same directory that the classes are in.

Now edit both .java files so that they have a package declaration:
Main.java:

Foo.java:

Note that you have to declare the package as "blah" because these source files are in a directory named "blah". If they were in a directory named "somethingelse" then you'd have to declare them to be in package somethingelse.

Now, to run the program, you have to be in the right relative position, directory-wise. So you have to back up out of the blah directory:

$ cd ..
$ ls
blah

That means the current directory would have blah as a subdirectory. Now you can compile and run like this:

$ javac blah/Main.java
$ javac blah/Foo.java
$ java blah.Main
Foobar!

Note the difference in the names used when compiling with javac versus running with java. When compiling, I used the directory path, with a "/". When running, I used the fully qualified class name, blah.Main. No .java, no .class but I included the package name with a dot.

This is how I did it on my Mac but you should be able to do the same thing on Windows, except use "dir" instead of "ls" and "cd" instead of "pwd".
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . you should be able to do the same thing on Windows, except use "dir" instead of "ls" and "cd" instead of "pwd".

Did you use pwd anywhere? As far as I can remember, cd works the same way on Windows® and Unix‑like terminals. Except for root folders; Windows® doesn't support a single root folder like / so you have to write something like d: without cd to move to the root of the d partition. Not certain about the last part.
 
reply
    Bookmark Topic Watch Topic
  • New Topic