I successfully built the Scala-only project template from the sbt android plugin. Next, I tried to add some Java sources to my project and got the following error in TR.scala. TR.scala is a file generated from the Java sources.
[info] Compiling 2 Scala sources and 5 Java sources to D:\Workspaces\MyProject\my-project\target\scala-2.9.0-1\classes...
[error] D:\Workspaces\MyProject\my-project\target\src_managed\main\scala\com\mydomain\myproject\TR.scala:2: object app is not a member of package com.mydomain.myproject.android
[error] import android.app.Activity
[error] ^
[error] D:\Workspaces\MyProject\my-project\target\src_managed\main\scala\com\mydomain\myproject\TR.scala:3: object view is not a member of package com.mydomain.myproject.android
[error] import android.view.View
[error] ^
[error] two errors found
[error] {file:/D:/Workspaces/MyProject/my-project/}My Project/compile:compile: Compilation failed
[error] Total time: 5 s, completed Oct 12, 2011 11:20:55 AM
Thanks to Yifan Yu's response in Google Groups, it appears to be a bug in the sbt Android plugin. Thanks Jan Berkel, for fixing the bug in this release.
You have a package path named com.mydomain.myproject.android, so
it's confusing the compiler when it tries to compile TR.scala in
'com.mydomain.myproject', because it thinks of 'android.whatever' as a
relative path. The plugin tries to prepend _root_. to the Activity
source it generates, but it forgets to do so for TR.scala.
Well, you didn't say exactly what you did ("add some Java sources"? how's that? cut&pasted code? which code? where? what?), which makes it hard to give a good answer. However, the explanation is actually simple from the error message.
It gives an error when you do "import android.app.Activity". Might that be the Java sources you spoke of? Well, I don't know. But I know you have either imported or are inside the package com.mydomain.myproject.android, because it says so in the message. In other words, you have one of the following:
package com.mydomain.myproject.android
import com.mydomain.myproject._
import com.mydomain.myproject.android
before the other import. What that means is that the following two lines will be equivalent:
import android.app.Activity
import com.mydomain.myproject.android.app.Activity
Which is probably not what you want. You wanted android.app.Activity to be an absolute reference, right? Well, it is not. You can make it absolute like this:
import _root_.android.app.Activity
Or you can just not have a package named android in your project.
Related
I have a project that exports a package for another project to import and utilize. Within Eclipse, it seems to resolve correctly. But when building with Maven, I get the following errors: The import x.x.x cannot be resolved. I've fiddled around with the manifest/ pom.xml and restarted Eclipse several times and the results are the same.
The general structure is:
com.my.company.plugin
- com.my.company.pkg
And within that manifest.mf, I have Export-Package: com.my.company.pkg
For the other plugin's manifest, I have Require-Bundle: com.my.company.pkg;resolution:=optional (I've tried taking the option portion out, but that didn't seem to matter)
And within the source for the second plugin, I have:
import com.my.company.pkg.MyClass
public class MyOtherClass implements MyClass {
Locally in Eclipse it all works fine. But when I try to build with Maven, I get the following error:
[ERROR] import com.my.company.pkg.MyClass;
[ERROR] ^^^^^^^^^^^^^^^^^^
[ERROR] The import com.my.company.pkg cannot be resolved
[ERROR] public class MyOtherClass implements MyClass {
[ERROR] ^^^^^^^
[ERROR] MyClass cannot be resolved to a type
The strange thing is that I have other imports within MyOtherClass that reference other plugins and added in the Required-Bundles. I'm guessing it's how I'm exporting the first plugin. What exactly am I missing?
---EDIT---
It seems like adding this block in the parent pom is causing it to fail. Which is odd because I thought it by setting it to ignore it will completely ignore the optional dependencies instead of trying to resolve it? While taking it out causes other areas that uses optional dependencies to fail...
<dependency-resolution>
<optionalDependencies>ignore</optionalDependencies>
<extraRequirements>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.ui</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.ui.views</id>
<versionRange>0.0.0</versionRange>
</requirement>
</extraRequirements>
</dependency-resolution>
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
I cannot directly use either of the following statements
import android.util.Log;
or
import android.os.Environment;
from InetAddress.java in libcore/luni/src/main/java/java/net, nor can I use any file which depends on it. A member of a group that I am working with has created a class which depends on android.os.Environment and has used it successfully in other places in AOSP such as packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
I'm not 100% certain there are not alternative methods to accomplish the tasks that the class he created is intended to accomplish, but for purposes of consistency I'd prefer to re-use his code in InetAddress.java if at all possible.
The error messages I receive are
Error: no package named android.os
Error: no package named android.util
I found this resource after searching for solutions on Google which suggested removing these lines to be able to import from standard libraries LOCAL_NO_STANDARD_LIBRARY = true;
Following this advice, I searched for parent directory closest to InetAddress.java which has an Android.mk file and tried editing it, saw that it had no lines that said LOCAL_NO_STANDARD_LIBRARY = true;
but it did call JavaLibrary.mk which did contain those lines. After commenting them out and rebuilding, the following lines appeared in the output after running make:
make: Circular out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes-full-debug.jar <- out/target/common/obj/JAVA_LIBRARIES/bouncycastle_intermediates/classes.jar dependency dropped.
make: Circular out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes-full-debug.jar <- out/target/common/obj/JAVA_LIBRARIES/conscrypt_intermediates/classes.jar dependency dropped.
make: Circular out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes-full-debug.jar <- out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar dependency dropped.
make: Circular out/target/common/obj/JAVA_LIBRARIES/okhttp_intermediates/classes-full-debug.jar <- out/target/common/obj/JAVA_LIBRARIES/conscrypt_intermediates/classes.jar dependency dropped.
make: Circular out/target/common/obj/JAVA_LIBRARIES/okhttp_intermediates/classes-full-debug.jar <- out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar dependency dropped.
make: *** [out/target/common/obj/JAVA_LIBRARIES/core-junit_intermediates/classes-full-debug.jar] Error 41
make: *** Waiting for unfinished jobs....
Fatal Error: Unable to find package java.lang in classpath or bootclasspath
make: *** [out/target/common/obj/JAVA_LIBRARIES/okhttp_intermediates/classes-full-debug.jar] Error 41
Fatal Error: Unable to find package java.lang in classpath or bootclasspath
make: *** [out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes-full-debug.jar] Error 41
I can post the full make output if it's necessary, but these lines are the only lines that seem to not be part of the normal output of the build process.
Answers or comments which provide alternatives to using android.os in the first place are certainly appreciated as that is the fallback, but I'd prefer to get these imports working if at all possible.
The problem is that the Makefiles are not working properly ... for what you are trying doing.
These errors:
Error: no package named android.os
Error: no package named android.util
... mean that those packages are not on the classpath when the compiler is compiling InetAddress.java. I suspect that this is by design; i.e. to avoid or minimize circular dependencies between the Java-subset packages and the Android-specific packages. It appears that you are trying to make or inject changes to InetAddress that break that build assumption; i.e. they create a circular dependencies between modules that are intended to be built in a ordered / partially ordered sequence.
Then this:
Fatal Error: Unable to find package java.lang in classpath
or bootclasspath
says that you've made things worse. But it is pointing in the same general direction - a compile time classpath problem.
How to fix it?
Frankly, I don't know if it can be fixed. But if it is possible, it is likely to entail significant reworking of the android modularization and its makefiles.
Note: this is not an issue with how imports work at the linguistic level. It is all about where the compiler is looking for classes to import (i.e. where the javac command line options told it to look) and whether those classes have been compiled (yet).
I created a new project on Play Framework 2.3.2 with Scala. I added two packages models and utils. Created a class in each package and made an import into the model from utils and I get this error :
[error] /media/hadareanrares/Media/Projects/TestingScala/app/models/Page.scala:3: not found: object utils
[error] import utils._
[error] ^
[error] one error found
Tried "_root_.utils._" and I'm getting the same result.
I seem to have fixed it by juggling with the class in the utils package.
I saw that IDEA IntelliJ detected the class in utils by importing as import _root_.ClassName, disregarding the utils package.
Then I moved ClassName to the root package, app. The compiler complained that it couldn't import _root_. so I moved the class back to the utils package and it seemed to have fixed it somehow after a rebuild.
The key thing that made it work was dragging the class from root to the utils package. It seemed to have updated links everywhere. But, have I not had the help of IDEA, how else would I fix this? I would like to use a simpler text tool, Sublime, that doesn't have the Scala integration that IDEA has.
You can try
activator reload // Reload the current application build file
and
activator update // Update application dependencies
then
activator idea // to recreate your IntelliJ files
I figured it out.. class in utils package didn't have "package utils" specified. Done.
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.