How do i link a compiler to an editor - java

So i have tried to find an answer but didn't manage to do it yet. What I'm trying to do is to write a Java code for example in Atom editor, and compile it on Atom itself, without the need of an integrated development environment. Same goes with adding classes to a project.
Is that possible?

Yes. That is how they wrote the first Integrated Development Environments. Although the compilation step is typically performed externally from the editor, if the editor can execute external programs then you can "integrate" compilation. Adding "classes" is a little more involved, in that it requires your "editor" to understand the packaging and structure of the files composing the classes - at that point you have an integrated development environment.

Yes it's possible with atom package build. To install the package Edit-->Preferences-->Install and type build and hit Enter. Then you should configure your custom build command as mentioned in official package website. You may just fill cmd: javac in yml file for example

You need to install a package that will compile. Atom is a hackable text editor but not really an IDE as you pointed out. That said there are a lot of packages for compiling and for working with java.
You can look at Build Tools Package for compiling. It looks like it will work for Java but there are many compilers in the package list.
I would search on their package site for the keyword "Java" to see what java packages you might want to use. Then do another search for "Compile" to see what compiler you might want to use if the build tool above doesn't work for you. You can search atom packages at https://atom.io/packages.

Related

How to import apache.jena into Visual Studio Code?

I am getting started with Jena and semantic technologies (I am taking a class on the topic). The lecturer recommends using Eclipse as IDE, but I would like to use Visual Studio Code. How can I import apache.jena? I would like to get information/error messages as you would with any other library.
I have had amongst VSCode's extensions, but did not find any support.
I include "import org.apache.jena.rdf.model.;" and get a "not found" error when I try to compile - unless I also include "-cp "/path/to/apacha/jena/on/my/computer/:." when compiling and running the program.
The goal is to be able to compile and run without having to include a path to the library and if possible, for VSCode to have an understand of the library (giving me warnings, suggestions, error messages osv.)
Use Java in VSCODE. You need to install the Extension Pack for Java, and the documentation contains more information.
Use VSCode to open the Java project folder, expand the project structure and display it on the JAVA PROJECTS panel.
Click the plus icon next to Referenced Libraries to add a .jar file reference. Of course you need to download the Jena library on your machine first.
Or use the following configuration in setting to add reference.
"java.project.referencedLibraries": [
"/path/to/jena/lib/*.jar"
]

Local Java package management system in Python PIP style?

I want to program in Java or other JVM languages like Scala, Kotlin, or Groovy. When I am programming on my projects, I only want to have import statements in my Java/Scala/Kotlin source files without the need to state the packages a second time in a Gradle/Maven build script. Instead I want to work as I would do in Python, i.e. have my import statements at the beginning of my source files and I am done.
The packages should then automatically included when I am compiling if all packages are installed in a central local package management system or otherwise get an error message telling me that I have to install a missing package. It should essentially work the same as for Python and PIP respectively.
Is a workflow like this possible preferably with Groovy or Maven?
Thanks in advance!
The closest I can think is Grape:
Grape is a JAR dependency manager embedded into Groovy. Grape lets you quickly add maven repository dependencies to your classpath, making scripting even easier. The simplest use is as simple as adding an annotation to your script:
#Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
Like in this example:
#Grab('net.sourceforge.nekohtml:nekohtml:1.9.16')
def parser = new org.cyberneko.html.parsers.SAXParser()
def page = new XmlParser(parser).parse('https://news.google.com/nwshp?hl=zh-TW&tab=wn')
page.depthFirst().DIV.grep{ it.'#class'=='title' }.each {
println it.A.SPAN.text()
}
I can't judge the Groovy landscape since I don't have any experience there, but for developing Java or Scala applications this exact workflow is not possible as far as I know.
Regarding "I only want to have import statements..." I think the closest you can get is good Maven/Gradle/sbt integration in an IDE, like IntelliJ that automatically adds the desired library to your build system's configuration file when using the correct shortcut. It (at least) works for some Java libraries when you're dealing with a Maven project in IntelliJ.
And regarding your other wish to have packages automatically included when compiling: On the JVM there is the concept of the fat JAR (also called uber JAR), which is basically a JAR (Java archive) that contains all dependencies and is thus self-contained. Usually you can start the application contained in it with a single java command.
To build fat JARs you need to have the approriate plugin for your build system:
For Maven that would be the maven shade plugin, see https://maven.apache.org/plugins/maven-shade-plugin/
For sbt you can use the assembly plugin, see https://github.com/sbt/sbt-assembly
Gradle probably has something similar
A lot of Java frameworks also come with their own build plugins that make building such self-contained applications relatively simple (Spring Boot is one example, but only suitable for applications that on top of an HTTP server)
Hope this helps, althought it's not an accurate answer to your question. :)

Using custom Java classes in Clojure REPL

In Eclipse, using the CCW plug-in, I want to load a clojure file into a REPL. The problem is that I have an import statement for one of my own java classes, but apparently it is not in my classpath.
(ns my-clj-ns
(:import [alg.gen Enumerator]))
Do I have to make jars out of every class that I want use/test in a Clojure REPL?
Currently, trying to load my clj into a REPL results in an error:
"Load file in Clojure REPL" did not complete normally. Please see the log for more information.
java.lang.NullPointerException
Any help would be greatly appreciated.
You can let leiningen compile these for you using,
:javac-options {:destdir "classes/"}
:java-source-path "src/main/java" ; location of Java source
options or manually compile them and move the class files to the classes/ directory. No need to create a jar.
When you're in the ccw repl, you can hit alt-e to see the stack trace. If you're getting a NullPointerException, I don't think its a classpath issue.
Your code looks fine to me.
I suspect the issue is with your Eclipse Java Build Path, which determines what Eclipse includes in the classpath for your application.
In particular, if your Java class is in a separate project, you will need to either add that project to the build path (right click on project / Properties / Java Build Path / Projects) or package it as a jar.
When you start to have more sophisticated build requirements, you may also want to start looking at Maven to handle this kind of thing for you. Maven is a pain to learn / set up in the first place but it pays of in the long run.
Leiningen is also a great tool to use but I personally don't use it for the following reasons:
It is great on the command line, but doesn't integrate so nicely with an Eclipse workflow
Maven is more widely used and better supported in the Java world
There is really nice guide if you want to learn how to do this.
https://github.com/technomancy/leiningen/blob/master/doc/MIXED_PROJECTS.md
But in gist, have a project definition like the following for Java source code.
(defproject megacorp/superservice "1.0.0-SNAPSHOT"
:description "A Clojure project with a little bit of Java sprinkled here and there"
:source-paths ["src/clojure"]
:java-source-paths ["src/java"])

How is a Java Project Structured (Compared to a Visual Studio C# Project)?

I'm trying to learn project automation and I'm using the book Pragmatic Project Automation as a guide. It's examples are in Java so I find it easier to follow it along in Java. I don't have any experience using Java or any of its IDEs. However, I've learned some C# using Visual Studio (although I'm still a beginner).
I'm having trouble understanding some parts of setting up a Java project. I'm using Netbeans IDE 7.0 on Windows 7. With Visual Studio, I have a solution with projects underneath. With Netbeans, I appear to just have a project with directories determining the rest of the structure (and the IDE to some degree)? The equivalent to Add Reference appears to be adding a source to the Classpath. There also seems to be a degree of separation between the compiler and the IDE. I'm currently in a situation where I can compile my project just fine while the IDE tells me I still have errors (and I assume this is because I have the project set up incorrectly).
I'm basically looking for analogies that will help me better understand Java project structure.
Lots of similarities between the two languages and IDEs. I spent many years in both. For starters the equivalent to 'add reference' in VS is adding a library or jar in netbeans. In respect to a reference - a jar is pretty much the same thing as a module or .dll in VS. A jar is a compiled reference. To add a reference just go to the project menu and then properties then to the libraries menu from there you can add either pre-assembled netbeans libraries, which are collections of .jar's, or a single .jar, or even a project. Once you add a reference you can import them into your class just like you would in C#.
Netbeans doesn't really have a 'solution' as VS does. You deal with individual projects. It does however have the capability to add a project as a reference so you don't have to continually re-build the references when you change something between multiple projects. It also has project groups to group similar projects.
Lastly Apache ANT is responsible for tying everything together in the background. Netbeans creates a build.xml and build-impl.xml file in the background to tell ANT how to assemble the project into a .jar.
There are my other things to cover but I thing this answers most of your questions. Does this help?
I can't speak for NetBeans, as I use Eclipse, but you are on the right track with classpath being roughly equivalent to references in the Visual Studio world. Libraries (usually .jar files) are placed on the classpath and must be there both at compile time and runtime (you specify the classpath to the compiler at compile time, and to the JVM at runtime). The classpath can contain many different entries, and they can be anywhere in the project structure (or outside of it entirely).
Java itself doesn't impose many restrictions on your project structure, although various IDEs and build tools do. The one thing that is a universal restriction in all Java environments is that source files (and class files) are placed in a directory named after the package name. So if your package name is com.test.something, then your source files will be in SRC_DIR/com/test/something, and your class files in OUT_DIR/com/test/something (note: SRC_DIR and OUT_DIR are not special variables; each IDE will have a different way to specify those directories).
Java libraires tend to heavily build-on one-another, so at some point, you'll find that the classpath entries are too many to manage manually. Before you get there, you'll want to take a look at Apache Maven or Apache Ivy which are dependency management tools. You'll need to understand how they work (either one, not both) and how to integrate them with your IDE. If you use Eclipse and Maven, m2eclipse offers fairly complete integration between the IDE and the dependency management tool.
With Netbeans, I appear to just have a
project with directories determining
the rest of the structure (and the IDE
to some degree)?
Visual Studio dictates a particular project layout and since the compiler is so tightly integrated into the IDE there's no real concept of a build script. In contrast, Java has no such structure (although certain 'best practices' have emerged such as having a 'src' directory for source files, 'lib' for libraries, 'test' for test source, etc.) and a build script is usually required to tell the compiler were to find source files and libraries, what artefacts to produce and a miscellany of other chores (running tests, deployment, creating code metrics and so forth).
In simple cases, the IDE will take care of this for you (if you follow the convention for that particular IDE) but ultimately you will probably want to take a look at a build tool to understand what's going on behind the scenes. Apache Ant and Apache Maven are both prominent offerings. Ant is very flexible whereas Maven attempts to dictate a common layout. I suggest you investigate both and see which suits.
There also seems to be a degree of
separation between the compiler and
the IDE. I'm currently in a situation
where I can compile my project just
fine while the IDE tells me I still
have errors
If your code compiles, it is correct. The IDE is simply acting in an advisory capacity (and will highlight issues beyond compiler errors, such as warning you of potential code mistakes or bad practice).
and I assume this is because I have
the project set up incorrectly
This is a possibility although, as stated above, there are many other explanations.

Tool for creating Dynamically Generated Code in Java (In Eclipse)

In Visual Studio land, I used to be able to define a structure in an XSD file and add a special attribute to it which would cause it to be dynamically compiled and available to use with intellisense in the other C# files in the application. I am not sure exactly what the term for this is, perhaps "dynamic code generation."
I am trying to accomplish the same in Java using Eclipse IDE. Basically what I am looking for is a tool that will allow me to specify some template and generate Java code from it in a "hot folder" that will allow me code complete in the other static Java files.
Does anyone know of a solution for this? I know it is possible in Visual Studio, but I can't seem to find anything for Eclipse.
Ok, here is exactly what I want to do.
Step 1. I create a folder called templates
Step 2. I create a file called HelloWord.ibes
Step 3. Code it automatically generated in my src folder HelloWorld.java
I want to be able to do this in eclipse easily.
You may create an ant build file that does the source generation for you. Then you are free to use any code generator you like. Ant support is part of the eclipse IDE. If you prefer maven, there's a nice eclipse pluging available (that's what I actually use for source code generation based on jaxb, javacc and xdoclet...).
Technically spoken, you just add another eclipse builder which is invoked anytime eclipse detects a change in your code base.
If you already have a code generator in mind, just 'ask' the internet if there's a plugin available.
Edit
On how to install a builder: This is done automatically. For maven, you just install the maven plugin (m2eclipse) and enable maven dependencies for a project. Then if you look at the projects properties pages (Builder section), you find a second entry in the list of builders.
It's similiar with ant, even easier, because ant is already integrated. "enable" ant for a project and the builder is added to the list of builders for the project. You can deselect it at any time if it kills performance or switch of automatic building (I don't know by heart how to enable ant builds for a project, but I remember that the eclipse help had sufficiant informations).
All about ant can be found here: Apache Ant
Creating a new builder is difficult, as it has to be coded in java and added to eclipse as a plugin. I bet you don't want to follow that track ;)
I'm not sure whether you have seen the code template option?
Preferences.Java, Code Style then Code Templates
How
to add code templates
Useful
code templates

Categories

Resources