Editing Java files outside of project in Eclipse - java

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.

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.

How to use JFreeChart?

It might be a silly question but I didn't figured it out how to use it.
I have downloaded JFreeChart from
http://sourceforge.net/projects/jfreechart/files/latest/download?source=files
and I don't use Eclipse or Netbeans or Intellij or any other. How can I compile my project within these files on command line ?
Thanks is advance..
Extract zip file you have just downloaded. Copy jars from lib folder to your lib folder and add all the jars to your classpath using -cp switch.
However what are you going to do then? If you do not use IDE you can write code using any editor you want however it is at least 10 times slower than using IDE. Managing dependencies manually and compiling code using command line compiler is possible too but it starts to be extremely complicated and time consuming once you have external dependencies (as in your case).
So, if you want to create something beyond hello world take you time and start working with build tool like maven or gradle and IDE.
Suppose that I have my project structure as following:
hello
src
Hello.java
classes
lib
one.jar
two.jar
In this case I have compile it using command
javac -cp ../lib/one.jar:../lib/two.jar Hello.java
run this command from src folder.
Use ; instead of : if you are on windows.

How to get the complete "java -cp xxxx com.xxx.class" command issued internally by eclipse?

Now my project works inside eclipse, but when I try to run it ,lots of jars are missing.
Alternative: Switch to Navigator view and open the projects file .classpath. All related projects, jars and user libs, that are used by eclipse to assemble the classpath, are listed in this xml file.
Better approach: add the following line to your code and capture the output:
System.out.println(System.getProperty("java.class.path"));
This is the classpath as seen from the applications perspective. You can use the result as a value for the -cp parameter (paths should be absolute).
There are a few option. In the package or project explorer you can right click, select "build path" and then modify the build path. This will open a listing of all jars on your build and execution class path.
Another alternative is to export the project as an executable jar, assuming you are okay with it jarring your program and all of the dependencies into a single jar. I think this is probably the ideal solution for you as it will produce a single file which may be double clicked to launch your application. Or, in a non GUI environment, may be run by executing java -jar MyJar.jar
The "File->Export->Java->Runnable JAR file" allow you to create a runnable jar with all the dependencies present. After invoking this, you will be able to execute your program as
java -jar my.jar
I would strongly suggest - for technical reasons - that you use the "Copy required libraries into a sub-folder next to the generated JAR" option.
You're asking the wrong question here. Eclipse almost certainly doesn't issue a command of that form at all. What you really want to know is what CLASSPATH Eclipse uses when executing your code. That's just a matter of what you have told Eclipse about in the way of library dependencies.
Do this in your code: System.out.println(System.getProperty("java.class.path"));
Oh.. someone beat me to it. Oh well ;)

How can I compile "import pack.*" with ant/javac, when there are no such classes?

For my company, I'm making a batch script to go through and compile the latest revisions of code for our current project. I'm using Ant to build the class files, but encountered a strange error. One of the source files imports .* from a directory, where there are no files (only folders), and in fact, the folders needed are imported right after.
It compiles perfectly fine in Eclipse, but I'm using an Ant script to automate it outside of the IDE, and Javac throws an error when it encounters this line. Is there any automated procedure I can use to ignore/suppress this error with javac in Ant?
I'd even go so far as to create a dummy file in the importing directory, but all of that in contained in a Jar file I don't wish to have to decompress and then recompress with the dummy file.
Having an empty (package) directory would not cause an error.
Make sure the (root) directory of that package hierarchy is being added to the classpath specifed for javac.
eg. if the package is com.stuff and the directory is /java/src/com/stuff then you need to add /java/src to the javac classpath.
Or just remove the import, if it is importing .* from an empty directory then it is redundant.
What is the error?
Maybe this is out of the scope of your question but have you ever thought about Continuous Integration solutions? We use LuntBuild and are quite happy (other alternatives exist as well: CruiseControl, Hudson, QuickBuild).
Check to make sure you're using the same version of the JDK in both Eclipse and from Ant. Perhaps this is a difference across JDK versions?
The only other option would be that it's a difference in parameters being passed to javac.
I'm betting it's the former, not the latter.
For building Eclipse projects outside of Eclipse, have a look at the ant4eclipse project.

Categories