Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Do Java from the console

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

If I wanted to set up java manually and write stuff in Notepad then compile it with the console, what do are the key concepts I need to know? I am using Windows.

Thanks in advance
Toni.
 
Marshal
Posts: 73738
332
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very briefly, what I personally would do:-

Don't use NotePad; download a decent text editor for programming; if I ever use Windows, I like NotePad++.
When you open the command line, give it the following instructionsYou can choose a different name for your directory, and you only need line 1 once, but use line 2 whenever you have any more work. You can create as many subdirectories as you wish. That keeps all your work in one place. Don't go into your Java® installation folder.
Make sure to update your system PATH to put your Java® installation folder as its first entry, and don't set a CLASSPATH.
The simplest way to run a single‑file application is java MyJavaFile.java. You are allowed several classes in the same file, but the runtime will require a main() method in the first class written. The procedure is different if you have multiple files.

If you get any error messages, find a good forum to ask about them. I recommend this forum.
 
Antonio Moretti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I think I am getting this now.

What is CLASSPATH and why shouldn't I set it?
 
Master Rancher
Posts: 4454
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do a lot of java work using an editor and batch files. I not use an IDE.
I rarely have to enter the command prompt.  
I use FileExplorer to create folders and files and give them the proper name.

As Campbell suggested, create a new folder to hold your java files.  
Do the following from FileExplorer:
Create the java source file.  For example: YourJavaSource.java
Click on the file and open it in notepad.
Enter the java source code and save it.
Create a batch file, for example: Compile.bat
Edit the batch file and enter these two lines:
javac.exe YourJavaSource.java
MORE
The first line calls the java compiler (Its location needs to be on the PATH, otherwise use the full path to the javac.exe file)
The MORE will hold the command window open so you can see the results of the compile.
Execute the Compile.bat file by double clicking.
If there were no errors, create another batch file: Execute.bat
Edit that batch file and enter these two lines:
Java.exe YourJavaSource
MORE
The first line tries to execute your code.
The MORE holds the window open so you can see the results
Execute the Execute.bat file by double clicking it
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:Thanks . . .

That's a pleasure

What is CLASSPATH and why shouldn't I set it?

It tells the compiler and runtime where to seek other code executed by your program as a dependency. Most programs don't need a CLASSPATH at all. Those that do use it need a different CLASSPATH for every program, so you will get no end of confusion if you have a System CLASSPATH. Also, a poorly set system CLASSPATH can make it impossible to find your ordinary java folder.
 
Antonio Moretti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that's really made things clear for me!
 
Antonio Moretti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Very briefly, what I personally would do:-

Don't use NotePad; download a decent text editor for programming; if I ever use Windows, I like NotePad++.
When you open the command line, give it the following instructionsYou can choose a different name for your directory, and you only need line 1 once, but use line 2 whenever you have any more work. You can create as many subdirectories as you wish. That keeps all your work in one place. Don't go into your Java® installation folder.
Make sure to update your system PATH to put your Java® installation folder as its first entry, and don't set a CLASSPATH.
The simplest way to run a single‑file application is java MyJavaFile.java. You are allowed several classes in the same file, but the runtime will require a main() method in the first class written. The procedure is different if you have multiple files.

If you get any error messages, find a good forum to ask about them. I recommend this forum.



Where are the compiled classes stored after I run the java command? I couldn't find them. Just curious!
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To the best of my knowledge, nowhere. I think they are deleted if you use single file execution.
 
Saloon Keeper
Posts: 24174
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:To the best of my knowledge, nowhere. I think they are deleted if you use single file execution.



What???  

The javac command puts the Xxx.class file in the same directory that it found Xxx.java in, if you're compiling from that directory. Alternatively, you can use the -d compiler option to tell javac to write the compiled class files to some other directory. A popular option is "-d classes", which keeps the generated stuff independent of the "permanent" stuff.

The java JVM executor never deletes the class files. One reason, in fact for an independent "classes" directory is that it's easier - and safer - to clean out a project by deleting everything in a generation directory like "classes: than to have to sort through a holds-everything directory with both source and class files.

Campbell has been using interpreted Java a lot lately, I think, and the rules are a bit different there. I think that's where he's getting the "deleted classes" from.
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . What???  

The javac command  . . .

The OP asked about the java command, not javac. As you said, the javac command puts the XXX.class files in the current directory unless other options are used.

Campbell has been using interpreted Java a lot lately, I think, and the rules are a bit different there. . . ..

Yes, the command OP asked about is a sort of interpreted Java®. The earlier versions of single file execution would fail if there were XXX.class files to confuse things. That is what I thought the OP meant when he said there were no XXX.class files after running the java command.
 
Tim Holloway
Saloon Keeper
Posts: 24174
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The original question was "write stuff in Notepad then compile it in console". To me that implies the traditional batch mode javac/java type of execution.

As opposed to "start java and type commands in immediately" - the interactive mode of operation. I should have used that word instead of "interpreted", but it escaped me at the time.

You  can presumably split the difference, write the app using an editor and then pipe the code into the java interpreter, but that's not what comes immediately to mind.
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can split all sorts of things, but let's not split hairs nor get too worked up about what we do and don't mean
 
Tim Holloway
Saloon Keeper
Posts: 24174
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think when the difference is whether classes get retained or not goes beyond mere hair-splitting, so it's good to be clear on which mode of operation is actually in effect.
 
Master Rancher
Posts: 1005
27
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I think when the difference is whether classes get retained or not goes beyond mere hair-splitting, so it's good to be clear on which mode of operation is actually in effect.



After years of acting like "REPL's are for wussies!" because none of the languages I worked in HAD them (sour grapes!), I have deeply fallen in love with Java 9+'s JShell, as can be seen by many examples of particular points of detail in my replies, normally quoting JShell output, which has only a small number of well-known behavioral differences from "normal compiled and run Java".

I watched some videos and took some notes on more advanced uses of JShell involving profiles and save states, I don't use those features too much usually, as most of my uses of JShell work just great from a new fresh prompt and some quick typing.

I am going to go back to "Direct Invocation of single-file .java source" from the command line soon, but not quite yet, as it is also covered on the OCJP.
In practice, I am so used to compiling and running as separate steps I almost always do that for things that don't fit well into JShell.

That will include calling it by whatever the proper name is, hopefully less of a mouthful than "Direct Invocation of single-file .java source" and less ambiguous than "Interpreted Java" which after all could include the whole run of simple code without a lot of loops due to the nature of JIT being more reactive than proactive.

There was a point way back when I thought that being both compiled AND interpreted was silly, I now Very Deeply Appreciate the many checks that the initial compile step does to root out all kinds of dumb mistakes that will fail compile before dragging me down a Runtime rabbit-hole.  I am thinking of you, directly interpreted languages.

For that matter, "Direct Invocation of single-file .java source" or whatever we are supposed to call it, still yields the benefits of the separate compile steps, it is just a "convenience method" apart from some restrictions on what code can be accessed and whether or not a .class file is created on disk, right?
 
Antonio Moretti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell said earlier that "The simplest way to run a single‑file application is java MyJavaFile.java." So I tried that but noted there was no class file. That's why I ask.

I did then find out if I tried the javac command first and created a class file, the java command wouldn't work because there was already a class file.
 
Norm Radder
Master Rancher
Posts: 4454
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the java command wouldn't work  


Do you get an error message? Can you copy the error message and paste it here?

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.
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:. . . the java command wouldn't work because there was already a class file.

Yes, the single file execution technique only works if the XXX.java file hasn't been compiled to XXX.class files first. I thought that requirement might have changed for more recent Java® versions, but that is obviously not so. You could try deleting the XXX.class files.
 
Antonio Moretti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly it make sense. Thank you!
 
Norm Radder
Master Rancher
Posts: 4454
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java command works for me with .class files present:

 
Saloon Keeper
Posts: 8434
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should of been able to do it with just
D:\JavaDevelopment\Testing\ForumQuestions13\Testing>java TestInhert.java

If not, then you need to tweak your environment variables.
Make a brand new environment variable named "JAVA_HOME" and set it to "D:\Java\jdk-14".
Then modify your existing "PATH" environment and place **at the beginning**
%JAVA_HOME%\bin;

You'll need to re-open your command window for it to work.
 
Norm Radder
Master Rancher
Posts: 4454
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You should of been able to do it with just


Yes, I understand about the PATH variable.  
I have several versions of the JDK and JRE on my PC.  The PATH does not point to JDK14.
 
Jesse Silverman
Master Rancher
Posts: 1005
27
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Yes, I understand about the PATH variable.  
I have several versions of the JDK and JRE on my PC.  The PATH does not point to JDK14.



When I didn't consider myself a Java Expert and went to seek help from those who were considered so, they understood that many of us had needs requiring multiple versions of Java being installed on a machine.

After, I suppose, too many times of having to debug weird interactions, they would only help people who removed the global/user settings of PATH (and I think CLASSPATH) and would set them explicitly in whatever we were running at the time.

It sure is nice if there is "Only the One" version of Java on a machine and that is good enough for everything.

When you are moving between more than one, it is rather easy to accidentally add the one you mean to be using "behind" another one.

I'd go to debug things for people (sometimes our "Java Experts" weren't available and all they had was me) and see two (or more!) versions of Java on their PATH.

I would ask them, "When you have competing versions of something in your PATH which one wins?  The first one, because it is first, or the last one, because it is last?"
When they couldn't answer or answered wrong, I'd quote the resident Java Expert and tell them they had to remove the machine/user-level PATH settings to get support, and give them .bat files or the equivalent to run before they used one version or the other.

Successfully combining a global setting and jumping between different versions is a little tricky to get right.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic