Play Framework - package does not exist - java

When I run:
compile
from play console I get the following error:
[info] Compiling 10 Scala sources and 7 Java sources to C:\Development\play\project\target\scala-2.10\classes...
[error] C:\Development\play\project\app\com\me\project\controllers\Application.java:27: error: package com.me.project.views does not exist
[error] return ok(com.me.project.views.html.index(null));
[error] ^
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code
[error] Total time: 4 s, completed 23-Mar-2014 19:00:24
However if I change that line to return ok("something"), then it compiles and I can see that the views folder is generated under:
C:\Development\play\project\target\scala-2.10\project\target\scala-2.10\classes_managed\com\me\project\views\html
so it looks as though the package should exist, no? I'm not quite sure what I am doing wrong.
Update 1
My source templates are located under:
com.me.project.views
Strange enough, when I can get the source to compile (as I mentioned above) eclipse will give me auto-complete on com.nat.aegis.views.html and I can see an index class located there too in the auto-complete dialogue. I have tried cleaning, refreshing and recompiling over and over but it is having no effect.

As mentioned in the documentation, introducing an HTML template file will lead to Play generating a class in the views.html package. The way you organise your templates into packages is then replicated below views.html by Play.
As a result, I think your return statement should be as follows:
return ok(views.html.com.me.project.index(null));
(This admittedly doesn't explain the folder structure you're seeing in your target directory.)

Solution
Ok I was porting a Scala project to Java and I noticed the following:
in the Scala project the way to reference a template is the way that I had in the example in the original question, however this is not correct in Java, the line should be:
return ok(com.me.project.views.html.index.render(null));
The compiler error didn't really allude to this but the response from #avik made me look closer at the docs (my brain is still in Scala mode).
[info] Compiling 10 Scala sources and 7 Java sources to C:\Development\play\project\target\scala-2.10\classes...
[success] Total time: 4 s, completed 23-Mar-2014 23:39:56

In my case I added a folder to the views folder named developer. to this folder I added a tables.scala.html file.
In my controller I would say:
public static Result main() {
return ok(developer.tables.render());
}
or
public static Result main() {
return ok(views.developer.tables.render())
}
During the run, the compiler said that the "developer package doesn't exists". I usually run clean, reload, and update when adding new views and classes to my project (using eclipse).
After a while, what did the trick was to reference this view like this
return ok(views.html.developer.tables.render())
Which if you ask me, does not seem very intiutive.
But this works, I hope it works for others as well.

Related

Java modules in IntelliJ and adding require java.ws.rs gives bizarre error

We have maven apps that until recently were on JDK8. We just upgraded them to JDK11 and are trying to take advantage of the JPMS from JDK9 by making our utility libraries into modules.
We originally had this kind of path:
utils/some-library1/src/main/java/com/company/team/utils/lib1/Util1.java
There, java is the "source root".
So a colleague placed the module-info.java file in the lib1 folder and declared it thus:
module utils.lib1 {
exports com.company.team.utils.lib1;
}
From the command line that builds and works, so he assumes everything is all module-y goodness.
But when I opened in Intellij, it had an ugly red line and the message said I should move it to source root. It then moved it to the "java" folder above. Fair enough.
That caused me to dig around trying to find out more about this JPMS that my colleague had implemented. After a lot of searching and experiments, I also determined that the "java" folder, as "source root", should be renamed to the name of the module ("utils.lib1"). So now I have these two files:
utils/some-library1/src/main/utils.lib1/module-info.java
utils/some-library1/src/main/utils.lib1/com/company/team/utils/lib1/Util1.java
And even Intellij is happy. Hooray! So I refactor all the other libraries. Suddenly I hit a major snag in let's call it lib2 with this line:
module utils.lib2 {
exports com.company.team.utils.lib2;
requires java.ws.rs;
}
Intellij flags the module with the red error squiggle again, this time saying:
Module 'utils.lib2' reads package 'javax.activation' from both 'jakarta.activation' and 'jakarta.activation'
I did some digging and found out the following:
java.ws.rs pulls in one of the following (it depends on which app):
javax.ws.rs-api-2.1.1.jar
jakarta.ws.rs-api-2.1.6.jar
Their module-info.java files contain this line:
requires transitive java.xml.bind;
Which pulls in one of:
jakarta.xml.bind-api-2.3.2.jar
jakarta.xml.bind-api-2.3.3.jar
jaxb-api-2.4.0-b180830.0359.jar
Which all have this line:
requires transitive jakarta.activation;
And that's where I give up. Our libraries are big hefty things that are hard to parse completely, so to simplify I created a maven app with just one class and all it does is import javax.ws.rs.core.Link.
And IntelliJ still gives that crazy error that I can't figure out and Google has been adamant in refusing to tell me.
Is it really broken or is Intellij just as confused as I?
I gave the long story both to show what we've done and to let you know that I'm very new to modules. So if it's a simple thing, please excuse me. I am just stumped though.
Additionally, are there any obvious tests one can perform at the command line to validate module configuration?
I've had inconsistent luck with jdeps, javac, and actually running as indicators of problems.
My suspicion is that things only work now because they're all in the unnamed module. But I need to get everything working if I'm going to convince anyone to change it.
EDIT
This question was reported as already answered, but that is incorrect. The suggested link deals with two different packages (A & B) importing package X. But in my case, the error is that the same package (A & A) imports package X. And this is done a few transitives down, so I have no control over the imports and can't see how to do an exclusion. Also, this problem can be repeated with just single requires statement in module-info.java!
Plus, there is a second question here that is also important that has not been addressed: how to validate the module configuration from command line (without involving the IDE at all).
I also determined that the "java" folder, as "source root", should be renamed to the name of the module
No, it should not. The java source root should be left as is but you must create a package name corresponding to your module name, so it should be /src/main/java/ - source root and then utils/lib1 directory - whidh would be the package.
I came across exact same warning in Intellij and it was genuine. In my case the collision was coming from three separate dependencies using same module name (i.e. 'jakarta.activation'):
'jakarta.activation:jakarta.activation-api:1.2.2'
'javax.activation:javax.activation-api:1.2.0'
'com.sun.activation:jakarta.activation:1.2.2'
It got it resolved for my project by applying explicit exclusions on dependencies which were pulling the last two.

Why Eclipse not showing any warning or error for a basic maven project

A simple code is written, I don't give ';' and '}' there must show a warning or error but it didn't show anything. When I try to import a user define class it does not show any user define class. I clean my project by clicking Project->Clean.. and I also check JRE and java compiler error/warning but everything looks fine. It is a maven project and normal projects are working fine. What should I do?
Put your class into src/main/java instead of src/main/resources.
J symbol for class indicates that Java source files are not on the build path, Since Java file is treated as an ordinary resource.
Move your class to src/main/java.
Eclipse JDT Icons

Intellij can't find java.net.http when compiling with Java 11

I'm trying to get one of my projects ready for Java 11 but for some reason Intellij can't find java.net.http. It isn't underlining it as not found in module-info.java like it would if I typed it wrong but when I try build the project I get the error below. I've tried reinstalling Intellij 2018.2.3 and uninstalling all other versions of Java. Any advice on how to get this working would be appreciated.
Error:
Information:java: Errors occurred while compiling module 'crawler'
Information:javac 11 was used to compile java sources
Information:15/09/2018 11:16 - Compilation completed with 1 error and 0 warnings in 636 ms
C:\Users\Will\IdeaProjects\crawler\src\module-info.java
Error:(2, 22) java: module not found: java.net.http
module-info.java:
module crawler {
requires java.net.http;
}
Request.java:
package Request;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Request {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("starting download");
String body = HttpClient.newBuilder().build().send(HttpRequest.newBuilder().uri(URI.create("https://example.com")).build(), HttpResponse.BodyHandlers.ofString()).body();
System.out.println("finished download:" + body);
}
}
Structure:
crawler
src
Request
Request.java
module-info.java
In the case that the above proposed resolution (by #Will) does not solve your issue as was the case with me (i.e. setting the project language level), check to to see what the bytecode target version of your java compiler has been set to, in your project preferences:
I had the wrong project language level set. To use java.net.http you need it to be at least 11. To change the project language level see: https://www.jetbrains.com/help/idea/project-page.html
Hopefully this helps someone else out.
I had the same problem with the package jdk.jfr.
This is how I fixed it. It should work for you too.
In order to make it work I had to make 2 changes:
First I had to set the language level to 11; see in the picture below.
Then I had to adjust the Java Compiler. The Target bytecode version is 11 and I set the project bytecode version Same as language level. Then you don't have to change all of them constantly. Please see picture below.
For those who are having this problem in 2022, even if the solutions mentioned here did not help, I was able to figure out what the problem was and how to fix this.
First of all I wanted to make sure that the problem is not from my Maven config, so I ran the following in my terminal:
mvn package
followed by:
java -cp target/covid-cases-cli-1.0-SNAPSHOT.jar org.matrixeternal.covidcasescli.App
and it was build without any errors whatsoever. So it means something is up with IntelliJ.
I am using Java 17 and building with Maven using IntelliJ. IntelliJ uses its own internal command to build the project. To override this behaviour, you must go to Preferences - Build, Execution & Deployment - Build Tools - Maven - Runner and select the option Delegate IDE build/run actions to maven which will essentially run directly from the Maven config file using the mvn tool installed in the system, instead of using the IDE command.
Set the compiler for IntelliJ as Java 11
IntelliJ Idea-> Preferences-> Build, Execution, Deployment -> Java Compiler
Select java 11 from the drop down

Java Error in SizeOf library

I saw this link which uses Instrumentation to calculate the size of objects during runtime. I decided to try this library since It can be really helpful in determining the size of big data structures.
So I wrote the following code in a new project named TrySizeOf using NetBeans IDE:
package trysizeof;
import net.sourceforge.sizeof.SizeOf;
public class TrySizeOf {
public static void main(String[] args) {
String s = "abc";
System.out.println(SizeOf.deepSizeOf(s));
}
}
After that I created a folder named lib inside my project, and placed SizeOf.jar in it. Then under Project->Properties->Run I placed the following parameter:
-javaagent:/home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
However, when I attempt to run my project I get the following error:
Error occurred during initialization of VM
Error opening zip file or JAR manifest missing : /home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
agent library failed to init: instrument
Can any one help me with this? or maybe explain what did I do wrong?
UPDATE:
I found the error, it was a problem with the upper and lower cases. When I started the project for the first time I made a little upper and lower case typo. When I paid attention to it I fixed it, but I kept getting the same error (However the path was fixed). Now, I tried using clean and build and the project worked. It was that when I do a clean and build manifest file gets updated which is used to specify the path of premain method. When I tried clean and build the file got updated and the project finally worked. Thanks for the comment about a typo in my post, which somehow made me think about this.

problems building the protobuf example apps

I'm new to protobufs and was trying to learn more about using them. I've downloaded the protobuf packaged from here. There is a README.txt file inside the examples folder of the archive which gives instructions on how to build 2 example applications. However when I follow those instructions for building the java application:
make java
I get a lot of errors followed by:
100 errors
make: * [javac_middleman] Error 1
All of the 100 errors seem to be classpath related, as this is a typical example:
com/example/tutorial/AddressBookProtos.java:37: error: package com.google.protobuf does not exist
Any ideas about how to get passed this?
The problem is that for some reason protobuf jar is not added to the classpath during compilation. To fix it you should open examples/Makefile and add -cp protobuf-java-2.4.1.jar at the end of java complilation line javac AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java.
P.S. If you built you protobufs with maven the jar is located at ~/.m2/repository/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar (version of the jar might be different)

Categories