Eclipse Project Compiling, Or Not? - java

I noticed that when I build even a very simple Java program in Eclipse, and I try to run it from the Terminal/Command Line and it gives me errors. I noticed after some hunting around that I have to actually compile the .java file I created in Eclipse in the Terminal to create and run the application. However, I can just save and run in Eclipse and get the same output (within eclipse).
I checked to see if I could build the project in Eclipse but the option to do so is greyed out. So, how can Eclipse run it if it actually never gets compiled?

Eclipse does compile it, otherwise it could not run it :-).
Eclipse generates normal .class files, just like javac. It puts them into its "build directory", which you set in the "build path" (or something - Eclipse not handy right now) dialog. By default its under /bin, I believe.
In principle, you can run your program in a terminal using these class files; you just need to set your CLASSPATH accordingly.
In practice, you would either run your program from inside Eclipse (which is for example easier to debug), or deploy your program (using e.g. Ant) to get some kind of installer or installation file, then install that and run it. That way you always run from a complete, correct installation.
Of course for small/simple programs, just running from Eclipse's class files is quite ok.

Related

VSCode doesn't automatically generate .class file for java

I'm new to java and learned that when creating a .java file usually there's a .class file generated automatically, which happened to the previous java files I created.
However, I forgot since when VSCode stops doing this when I create new java file.
Another problem is, when creating a new java file, the shortcut to type "main" and press enter doesn't generate
public static void main(String[] args) {
}
anymore. I have to literally type out the whole thing, otherwise I have to close this new file, open again, wait a few seconds to half a min or so for the shortcut to work.
Any reason why?
The .class file is generated by compiling the .java file. The following settings in settings.json control the generation of .class files in the bin directory.
"java.project.outputPath": "bin",
In addition, you need to download the Extension Pack for Java, read the official document for more help.
Also check the following settings to control the location of code snippet suggestions.
"editor.snippetSuggestions": "inline",
Sounds like you've used some sort of IDE before, maybe IntelliJ or Eclipse.
The .class files
The .class files are compiled Java source files, containing JVM bytecode. These are generated when you build your Java program, either via a build tool (Maven, Gradle, Ant, etc..) or by compiling the sources. Now, if you use an IDE in most cases the IDE will take care of building your project. If you use the stock VSCode without any Java related plugins, VSCode doesn't know how to build a Java project out of the box. I believe you can define a build task, and run that, but it doesn't support it out-of-the-box, without any plugins. So you should look around in the VSCode plugin marketplace what Java-experience-enhancing plugins you can add.
Code snippets and shortcuts
Not sure why you have to reopen files for shortcuts to work. That being said, you're looking for code snippets, or IIRC IntelliJ calls these live-templates. These are, well, templates for code generation, which you can invoke in your editor. IIRC VSCode doesn't have any Java related code snippets, you have to add them yourself or install a plugin that provides these. In IntelliJ, you have built in templates or snippets for stuff like the main function, for-each blocks, etc.. but again, IntelliJ is a JVM-focused IDE, a very good one too. VSCode is a really good tool, but you may have to install some plugins and add stuff in order to have the cosy IDE-like experience.

Why are my Java code changes not being reflected when ran from command line but working in Eclipse?

I am using a bash script to run my Java program that I made in Eclipse and the Java program is working fine when ran from Eclipse. It has my most recent changes which I can tell by some print statements that I just inserted and ran again.
However, these print statements and all my other changes are not being seen when I run my bash script, which literally just runs the program like this (using testNG):
java -cp ".\src\main\java;lib\*;" org.testng.TestNG ParallelTestXML.xml
I have already cleaned the project in Eclipse and made sure build automatically is clicked, although I think that is to fix if it isn't compiling recent changes within Eclipse. So I have no idea what else it could be.
Because .\src\main\java doesn't do anything useful.
Eclipse has this concept called 'builders' and 'project kinds', and depending on how you've set up your java project, eclipse's build-on-save architecture works differently.
Assuming you just went: "File > New Project > Java Project", and picked all the default options, the way eclipse is set up is that you have a src dir (the fact that you write src/main/java belies that you didn't do this, but I'll continue for the sake of example), and when you save any java file in eclipse, eclipse will immediately update a built view of this, and it will be in a dir hanging off of the project root called bin.
That's where the class files live, so if you want to run off of those on the command line, the right move is:
java -cp ".\bin;lib\*;" org.testng.TestNG ParallelTestXML.xml
Adding the src dir is completely pointless, unless the class files live right next to the source files, in which case calling that dir src is obviously very silly (as a general rule in programming, picking a name that clearly lies, is a very bad idea, for obvious reasons).
If you have some other project setup, for example, you've set it up as a maven project or a gradle project, well, it depends on how you configured eclipse whether eclipse is trying to 'match' the builds, or is triggering a full maven build every time you save, or if you're supposed to invoke maven manually. Most likely the latter. Let maven do the building, and maven will then build your stuff someplace. Generally, {projroot}\target\classes, but to 'run' your app if your app is built with maven, don't invoke java. invoke mvn, asking it to test your stuff. That way mvn will take care of your deps and the like.

Create an Exec File in Eclipse

I'm sure the answer is really obvious and right in front of my nose, but I am writing programs in Eclipse and I'd like to be able to run them outside of the workspace. (Like on the command line.)
Problem is I can't find the executable file in the file explorer. So I have to ask....
How do you run programs you write in Eclipse OUTSIDE Eclipse? (Program is in java.)
I just recently switched from Visual Studios to Eclipse so I don't really know Eclipse well. Most of the time figuring out the IDE is more hard to actual coding really.
The executable can be created by Clicking: file -> Export -> As Runnable Jar File
Then make sure launch configuration is set to the correct project, and that the export destination is where you want it to be. Then Click finish, and your runnable jar file should appear where you specified.
If the program has a GUI, it can be run like a regular exe file (double clicking, etc.) if it does not have a GUI it will need to be launched from the command line (ie, by navigating to it with CMD, or with a batch file).

Editing Java files outside of project in Eclipse

I have to write some little programs in Java for school, so I don't want all that stuff that Eclipse generates with a new project. The way I'm doing it now is this:
$ touch myprog.java
open and edit myprog.java in Eclipse
$ javac myprog.java
$ java MyProgClass
The problem is that Eclipse doesn't show warnings and errors while typing the code. It would also be nice if it would let me compile and run the file inside Eclipse (by doing what I do above in the working directory).
Is there any way I can make Eclipse do this?
This is because Eclipse doesn't consider your .java file as something it should compile (and thus generate errors for)
You need to mark the folder containing myprog.java as a source folder. Here's how you do it:
Right-click on the folder and choose "Build Path" -> "Use as source folder".
Well because now it's just a text file for eclipse and it isn't linked to java project.
Create new project and put myprog.java to /src folder in eclipse project.
Then:
$ javac workspace/yourpoject/src/myprog.java
$ java workspace/yourproject/bin/myprog
As a result copy only those 2 files and ignore the rest of project files if you want.
Can't you use an existing project into which to create school classes?
Alternatively the NetBeans IDE is somewhat slimmer. You would use Run File there.
The short answer: no, you can't make eclipse do that.
Eclipse JDT needs to know the classpath to compile (even if that classpath only includes the JRE), builders to tell you if there are errors, and the search engine for standard IDE things like content assist or open declaration.
It's trivial to create one java project, and then use that to create all of your little java programs. They compile correctly, report errors, and are easy to run ... and if you want to run them from the command line as well, there's nothing stopping you.

Exported runnable JAR file from Eclipse doesn't seem to execute

(Disclaimer: I am not a Java programmer, and really have very little idea what I'm doing in Eclipse)
I hacked out an application in Eclipse a while back which I finished off and exported as a JAR which runs nicely. I didn't take any notes or make any comments.
The time comes to make some changes to the project but I have to reload it all again because my PC has since been wiped so some settings etc have probably changed.
Anyway it all works and runs nicely from Eclipse. But when I export as a runnable JAR, and double click to run the application, it doesn't seem to execute.
I can see that there is an instance of javaw running, but no window appears. No errors. Nada.
As I said, it works beautifully in Eclipse. Any ideas?
I have little idea of how I can detect what is going on - as I said, I am NOT a java programmer :-)
Ok then, so you can mark the question closed:
If your Java installation is registered for the .jar extension it will try to run the Jar (i guess that's what's happening). But it will do so quietly.
In order to see what's happening (errors and such) you should try to run it from the command line with java -jar myfile.jar.

Categories