Groovy, Netbeans and Java EE - java

I want to develop a web application (no frameworks) mixing java with groovy. I am using the IDE Netbeans with the plugin.
If I start a new Java SE project and add a groovy class, it works with no problems.. but when I create a new java EE project and add a groovy class it can't compile and shows me the following error:
/home/webcodei/NetBeansProjects/testeGroovyWeb/src/java/pacote/Hello.java:23: cannot find symbol
symbol : class Hroovy
location: class pacote.Hello
Hroovy h = new Hroovy();
/home/webcodei/NetBeansProjects/testeGroovyWeb/src/java/pacote/Hello.java:23: cannot find symbol
symbol : class Hroovy
location: class pacote.Hello
Hroovy h = new Hroovy();
2 errors
/home/webcodei/NetBeansProjects/testeGroovyWeb/nbproject/build-impl.xml:383: The following error occurred while executing this line:
/home/webcodei/NetBeansProjects/testeGroovyWeb/nbproject/build-impl.xml:211: Compile failed; see the compiler error output for details.
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
Does anybody have a clue of how do I enable Java EE + Groovy in netbeans?
ps: I know the existence of Grails
ps2: The Groovy jar is in my classpath.
Thank you for all!

It appears that the NetBeans 6.5 Java Webapp project manager does not have the "Enable Groovy" support that is present in the Java App and Java Class library projects.
I can think of two ways you might get around this:
First, you could put your Groovy code and tests in a separate project as a Java Class Library. Then make the Java webapp dependent on the Groovy project. NetBeans will build the dependent project automatically so you'll hardly notice they are in separate projects.
Second, the "Enable Groovy" isn't magic. All it does is write a groovy-build.xml in /nbprojects and modify build-impl.xml to import it. The groovy-build.xml overrides the default "javac" macro to invoke "groovyc" instead. If you're at all handy with Ant, you could copy a groovy-build.xml from a Java Application project and copy it to your Java Web project and then import it from your build.xml (before build-impl.xml is imported). The groovy-build.xml would likely need a few tweaks as some of the properties between a webapp and class library are a little different.

#Dave Smith,
This was exactly what I did. I created one javase project and one webapp and started to compare them. After a few minutes I realised that the only diference was the groovy-build.xml.
So I copied the groovy-build.xml into the dir, and inserted the following lines into my build.xml:
<import file="nbproject/groovy-build.xml"/>
Right before the regular
<import file="nbproject/build-impl.xml"/>
And then called the groovy file to overwrite the -init-macrodef-javac.
<target depends="-groovy-init-macrodef-javac" name="-pre-compile">
</target>
I also needed to change the namespace from the groovy-build.xml to mine ex:
<macrodef name="javac" uri="http://www.netbeans.org/ns/web-project/2">
And inserted the j2ee classpath (${j2ee.platform.classpath}) to the attribute a few lines later:
<attribute default="${javac.classpath}:${j2ee.platform.classpath}" name="classpath"/>
After that the project worked successfully! =D
Thank you for all!

Related

How to Start a Java Project with package declaration on Visual Studio Code?

I am learning Java on Visual Studio Code. I have installed the "Microsoft extension for Java" in it. My basic Java programs runs fine without package declaration. But I would like to package my program. How ?
Earlier I used "IntelliJ IDEA". I used to start a New Project and declare "package com.java.learn". In Visual Studio Code there is no option to create New java Project. There is an option to create Workspace but I still have the same issue.
I have two java class. "Index.java" & "InputHelper.java". Index.java is the main java file. InputHelper is a seperate class which I use in Index.java. I want to make a project and package both ( or more ) files.
Error Message:
The declared package "com.java.learn" does not match the expected package
A package is a path of subdirectories. Say your java sources are in (subdirectory of) a directory src. All sources immediately under src have the "default" package = no package declaration.
In src/com/java/learn (4 nested directories) the package com.java.learn; is expected for java sources.
In your case create a path of 3 directories: com, java, and learn the latter containing your java source.
For the rest, try to follow the coding conventions of java: class names starting with a capital like Index, variable and method names with a small letter.
In fact though Microsoft is often underestimated, I would chose a more mainstream IDE for learning java. IntelliJ IDEA (Community edition) is fine; NetBeans IDE is a clean an nice IDE too; eclipse is used very often - though a bit overdone IMHO.
I faced a similar issue, coming from Eclipse/IDEA background you find it difficult to not have a feature in your java IDE to create a new package.
Although, Joop Eggen's answer is correct that package is a path of subdirectories but you might find it tedious to create subdirectories when the number of sub packages is greater and name of sub packages is long.
You can use the below VSCode extension :
https://github.com/jiangdequan/vscode-java-saber
It is a very handy extension.It provides support for:
New: Java files(annotation/class/interface/enum/package/JSP/HTML)
Generate Getters and Getters
Copy Qualified Name
Sort Project By Name
Run Maven Goals
Generate Docs
You can try this extension.
You can use Java Projects panel to create a new project, package, class.
Also I think there's an issue in VSC 1.63.2, because a new item is created but it's not displayed in project structure until I reload VSC window.
Another option is to put right package declaration on the first line of a class file and use inline 💡light bulb button to move that class to the package it belongs.
P. S. I'm learning Java now so I could be missing something

Jruby extend java class in another directory using Rubymine

I am somewhat new to RubyMine but here is my problem. I have a JRUBY Class that I want to extend from a Java class. My Java class is part of a submodule I have imported using git. This is my project structure:
src --> Submodule --> <Directories> --> ClassIWantToExtend.java
--> Ruby Code --> <Directories> --> JRubyClassThatExtendsJava.rb
However, when using RubyMine I have been unable to figure out how to extend this Java class. It can't seem to find it. My current class is blank. This is all I have:
require 'java'
class JRubyClassThatExtendsJava
end
I have tried using '<' and 'include' but when autofilling RubyMine can't seem to find my Java Class. I did just add the submodule using a CLI Git Command. Is it possible I have to add something for RubyMine to see it?
Thanks for any help in advance.
RubyMine, as far as I know (bought a license but never actually used it due this) does not include support for .java (not even a syntax highlighter) - the motivation seems to be that they have a separate product that is a Java IDE. thus this answer is not going to be RubyMine specific :
first you'll need to compile the .java sources and either pack them in a .jar or simply keep in mind what directory the .class files are (javac -d OUT_DIR)
than in your .rb you can either require 'path/to/packed.jar' or simply $CLASSPATH << 'path/to/classes/OUT_DIR'
... than you should be able to load the Java class and extend it in Ruby

"taskdef class JFlex.anttask.JFlexTask cannot be found" when building Soot

I'm trying to build soot.
I've downloaded the source code of JastAddExtensions using svn co https://svn.sable.mcgill.ca/abc/trunk/JastAddExtensions/, checked out the code of Soot using
git remote add soot https://github.com/Sable/soot.git
git pull soot develop.
Then I tried to build Soot using Ant and got following error message:
D:\dev\ro-01\soot\build.xml:102: The following error occurred while executing this line:
D:\dev\ro-01\JastAddExtensions\SootJastAddJ\build.xml:18: taskdef class JFlex.anttask.JFlexTask cannot be found
using the classloader AntClassLoader[]
How can I fix this error? Where can I download a version of code of JastAddExtensions\SootJastAddJ, which is compatible with current version of Soot?
You will get help faster on the Soot mailing list. You do not need do build the JastAdd part of Soot. It's pre-built in the directory "generated".
Check out:
http://jflex.de/jflex_anttask.html
Simple answer: You probably haven’t copied the JFlex jar to ant’s library folder/directory.
If that doesn’t straighten you out… More complex solution: Find the soot Ant file, probably in the project root directory as build.xml.
Then find a task definition named “jflex” that probably looks like this:
<taskdef classname="jflex.anttask.JFlexTask" name="jflex" />
Change it to something like this:
<taskdef
name="jflex"
classname="jflex.anttask.JFlexTask"
classpath="${compiler_tools_path}${jflex_jar_name}"
/>
Up near the beginning, with the other properties, create a new one with the location of a directory/folder you create and copy the latest JFlex jar to:
<property
name="compiler_tools_path"
value="/Library/CompilerTools/"
/>
This is an appropriate place to create a directory/folder to store this on a Mac. Windows and Unix, anywhere externally installed jars are stored.
<property
name="jflex_jar_name"
value="jflex-1.5.1.jar"
/>
An example of a recent JFlex jar build. Check out:
http://jflex.de/
I hope this was helpful.
Jeff
I took a look at build.xml in the soot repository. The problem is with the target jastadd. The JustAdd compiler uses Beaver as its parser generator, which also requires JFlex as it's scanner generator for handling .flex files.
Just follow my simple solution mentioned previously. Make sure you rename the jflex-1.5.1.jar file that you copy to Ant's lib directory to "JFlex.jar" It's all in the document I gave you that first link to.
I'm answering this problem so late because I only just came across a similar problem with getting ant files to work with JFlex in Eclipse projects.
Jeff

Import Java library in RIDE

I'm trying to use a java library in RIDE. I found a good tutorial( https://blog.codecentric.de/en/2012/06/robot-framework-tutorial-writing-keyword-libraries-in-java/)
I follow it, but when the time comes to import and use the java library ( Database Library)in RIDE. It fails. When I look the page with my different imports, the java library is written in red and not in black as the others.
And when I try to run with Jybot, I have the well-known message :
[ ERROR ] Error in file 'C:\Users\XXXXXX\Documents\Robot_Test\implementation\DB_Test\Example.html': Importing test library 'org.robot.database.keywords.DatabaseLibrary' failed: ImportError: No module named robot
I follow every line of the tutorial, even the with the set CLASSPATH.
Any idea ? ( I know that this library exist in Python, but I want to write my own java libraries ^^)
Thanks
This worked for me using:
Jython 2.7b4
Robotframework 2.8.7
Ride 1.3
Create Lib and compile it (you do not need to jar it)
Directory structure is
run_ride.sh
libs/DemoLib.class
tests/DemoLibTest.txt
Excerpt from tests/DemoLibTest.txt:
* Settings
Library ../libs/DemoLib.class
* Test Cases
DemoLibTest
Print Demo
Start Ride, switch to tab "Run", choose Execution Profile: jybot, press Start, output is:
Starting test: tests.DemoLibTest.DemoLibTest
20150304 19:13:20.321 : INFO : ---------- Demo ---------------
To avoid confusion put this line
echo $CLASSPATH
in your Ride startup script in order to ensure that your library is really imported. By the way, in my Ride the import is also marked red. Sometimes restarting Ride might help. But the colour does not mean anything, if your settings are correct.

Debugging java file compiling (using gwt)

I was asked in a precedent question to be more precise about my compilation error message. Here's the fact : I know nothing about GWT and Java. Following the docs, I tried to compile Java files from a web project that had been precedently developed using GWT. So, to test and understand how all this work, I took the java folder (that had been precedently compiled with an appropriate tool)
into src folder of a web app project in my ide Eclipse
When running the compiler using the command GWT Compile, I had this message error :
Compiling module java.org.primagora
Validating newly compiled units
Ignored 5 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Finding entry point classes
[ERROR] Unable to find type 'org.client.primagoraEntryPoint'
[ERROR] Hint: Check that the type name 'org.client.primagoraEntryPoint' is really what you meant
[ERROR] Hint: Check that your classpath includes all required source roots
When I look at the error on the file, for example java.org.client.primagoraEntryPoint, I find an error when it is declared "package org.client" at the very beginning of the file. There seem to be an error path. I thought the java folder I took would be correctly implemented in Eclipse.
Is that clearly a file path problem ? How should I debugg it ? (I reallly know nothing about gwt, java , eclipse)
Best,
Newben
Where is your GWT module file (i.e. a file that ends in .gwt.xml), and what are its contents? The package you list for your entrypoint is org.client, and the full name is org.client.primagoraEntryPoint, suggesting that there is a file in org/client/primagoraEntryPoint.java. By default, module files include the client package relative to them as source, so if the file is in the wrong location, this won't work correctly.
Based on this, your module file should be in the org package:
src/
org/
SampleModule.gwt.xml
client/
primagoraEntryPoint.java
From the very beginning of your error, you list java.org.primagora as the module:
Compiling module java.org.primagora
This suggests the following structure:
src/
org/
client/
primagoraEntryPoint.java
java/
org/
primagora.gwt.xml
This doesn't make sense, since GWT is now looking for a java.org.client package instead of a org.client package. Try using the package setup mentioned earlier here.
A better option might be to pick an existing working project, like what the GWT plugin for Eclipse will create, or one of the samples in the GWT download.

Categories

Resources