Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Harnessing the Power of Objects Constants

Unable to run JShell

After I run JShell, I ran the code:

/open PezDespenser.

just as Craig demonstrated. But it gave me an error message:

File 'PezDespenser.' for '/open' is not found.

I then tried to run it with the lines of code below:

/open PezDespenser
/open PezDespenser.java

and when I ran /open DezDespenser.java, it gave me more other error messages:

jshell> /open PezDespenser.java
| Error:
| '{' expected
| class PezDespenser;
| ^
| Error:
| reached end of file while parsing
| class PezDespenser;
| ^
| Error:
| illegal start of expression
| public final int MAX_PEZ = 12;
| ^

But when I compile and run my program via the console, it runs just find without any error messages. Is this a bug or am I make a mistake somewhere? Thanks in advance.

14 Answers

No problem, and actually that's a good shortcut to know. Thanks for your help Craig.

I took a snap shot with the camera icon ,and it took me to this URL

https://w.trhou.se/mnb94m6uai

is that correct?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

This line looks suspect:

class PezDespenser;

It should open scope with a curly brace. Do you want to snapshot your workspace and I can take a look? The camera icon up top.

My class does open with a curly brace.

http://i67.tinypic.com/6hq8mc.jpg

Nicholas Brigham
Nicholas Brigham
780 Points

I have the same issue and my class also opens with a curly brace like omid.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey y'all can you do me a favor and try the style of putting the { on the same line as the statement. I think it might be the problem.

I placed the curly brace on the same line, but it still gives me the error message.

http://i65.tinypic.com/34in14h.png

Craig Dennis
Craig Dennis
Treehouse Teacher

Omid can you snapshot your workspace please?...there is a camera icon at the top that will let you share your actual workspace with me.

Craig Dennis
Craig Dennis
Treehouse Teacher

Hey Omid! I forked your workspace and this worked:

jshell> /open PezDispenser.java                        
|  File 'PezDispenser.java' for '/open' is not found.  

jshell> /open PezDespenser.java                        

jshell> PezDespenser pd = new PezDespenser("Yoda");    
pd ==> PezDespenser@56ef9176                           

jshell> pd.getName();                                  
$3 ==> "Yoda"                   

Note the misspelling of the filename and the class, but it does indeed work.

Do you see those same results?

Hi Craig, I fixed the misspelling of my file names to PezDispenser.java and PezDispenser.class. But unfortunately when I run:

/open PezDispenser.

or

/open PezDispenser.java

It continues to give me error messages....

I'm confused why this is not working. Can I reset my Java track? I think this might be a bug or glitch.

Craig Dennis
Craig Dennis
Treehouse Teacher

Can you show me the errors?

It's actually one error. I said errors before to imply that nothing seems to fix the issue. But its the same error as the previous times.

http://oi68.tinypic.com/rqw4n7.jpg

Craig Dennis
Craig Dennis
Treehouse Teacher

In that screenshot you have not typed the .java extension.

Yes I know, but that's because I was following what you did in the video. In the video you typed /open PezDispenser. "without the extension"

Then on the next line, these showed up PezDispenser.class PezDispenser.java

Then you typed /open PezDispenser.java the second time "with the extension"

It works now when I type /open PezDispenser.java. But in the video why did you type the statement the first time without the extension? Then the full statement with the extension the 2nd time?

Craig Dennis
Craig Dennis
Treehouse Teacher

I pressed tab to autocomplete...sorry for the confusion!

Inbar Shimshon
Inbar Shimshon
2,286 Points

I have the exact same confusion here, please avoid missing steps like this because I am still getting this error when writing "http://screencast.com/t/AZpVanbv" and its not clear from the video at all what you did there.

Craig Dennis
Craig Dennis
Treehouse Teacher

Looks like you have an error in your code... try to compile and fix the code. Looks like you left code in that changes the final field.

Inbar Shimshon
Inbar Shimshon
2,286 Points

Hi Craig, thank you, I compiled and got the following error: Error: Main method not found in class PezDispenser, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

I am not sure what I am overlooking in comparison to the code in the video: http://screencast.com/t/FYnEg85Abk

Thank you for your assistance!!

Craig Dennis
Craig Dennis
Treehouse Teacher

Sorry just try to run it the app, not the PezDispenser class. It's looking for your main method which is in Example.java.

I can't see the command you ran in the screenshot there but assuming you called jav PezDispenser. That error is saying that the class doesn't have the main method which is required to kick off programs.

If you are still having problems, you can snapshot your workspace (camera icon) at the top, and we can dig deeper.

Inbar Shimshon
Inbar Shimshon
2,286 Points

Hi Craig, thank you for your reply! see a screencast video here http://screencast.com/t/cg5sjZNJo47L

Also when running on jshell this code via /open PezDispenser.java it just loads nothing

My apologies for the many questions..

Craig Dennis
Craig Dennis
Treehouse Teacher

Use the workspace snapshot inside workspaces please. It's on the top bar. Then post the link here, then I can actually fork and run your code.

We will get it sorted, no need to apologize!

Inbar Shimshon
Inbar Shimshon
2,286 Points

okido, here you go https://w.trhou.se/1ybe3u17ty

I just copy pasted the link, didnt run any code (not sure if that was required)

Thank you very much Craig!

Craig Dennis
Craig Dennis
Treehouse Teacher

Is it not working like this for you?

jshell working as expected

Let me know if those exact steps produce something else...that is how it is intended to work.

Inbar Shimshon
Inbar Shimshon
2,286 Points

Yes! it works, I was not following all the full instructions in Jshell like new PezDispenser ("Yoda");

Thank you for your patience Craig, you are super nice!

James Adamski
James Adamski
3,027 Points

I have wasted a couple of hours trying to get this to work too!
If I try to open PezDispenser with jshell, I get what is essentially file not found.

If I try to follow your lead and use autocomplete the I still get a file not found error. The 124 possible completions are essentially a listing of the instance from the root on down to the home location. It appears that you don't have any environment variable set that actually refers to the /home/treehouse/workspace directory as a "known location" on the PATH variable.

Wouldn't it be prudent to have the working directory in the PATH for searching? It's an old trick but it should work.

----- My error ----
jshell> open PezDispenser.

<press tab again to see all possible completions; total possible completions: 124>

--- My error with other syntax-------------- jshell> open PezDispenser.java
| Error:
| ';' expected
| open PezDispenser.java;

--------- PATH Variable does not include workspace--------------- treehouse:~/workspace$ echo $PATH
/home/treehouse/bin:/usr/local/nodejs-binary/bin:/home/treehouse/.local/gems/bin:/home/treehouse/workspace/bin:/usr/local/pyenv/shims:/usr/local/pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/opt/ go/bin:/home/treehouse/.local/bin

Craig Dennis
Craig Dennis
Treehouse Teacher

Hi James Adamski

Can you give me a workspace snapshot please? The little camera icon above. Sounds like your code is missing a semi-colon.

The correct syntax is:

/open PezDispenser.java

Thanks!

James Adamski
James Adamski
3,027 Points

Here is what worked for me. It was the /open and not the semi-colon that made the difference.

jshell> /open PezDispenser.java

jshell> PezDispenser pd = new PezDispenser("Yoda");
pd ==> PezDispenser@28feb3fa

jshell> pd.MAX_PEZ;
$3 ==> 12