Gradle fails trying to build class using PsiMethod in IntelliJ Plugin project - java

I have a Intellij plugin project that I can successfully build without using the PsiMethod class. I've generated this project using this intellij plugin guide
As soon as I add in code using PsiMethod class
(from the Intellij SDK code_samples directory)
The intellisense cannot find any GOTO information for the class.
I can then switch my project settings module from JAVA_1.8.0 to
by adding it into the platform settings SDKs:
This now adds intellisense, and gives me the ability to navigate to the PsiMethod class by CMD+click.
However, If I try and run my "plugin" gradle configratuion. it halts with an error running :runIde.
Running compileJava command from bash I get:
$ ./gradlew compileJava
> Task :compileJava FAILED
/Users/cody/IdeaProjects/plugintest/src/main/java/com/plugintest/TestClass.java:104: error: cannot find symbol
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
^
symbol: class PsiMethod
location: class ParticleContainerManager
/Users/cody/IdeaProjects/plugintest/src/main/java/com/plugintest/TestClass.java:104: error: cannot find symbol
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
^
symbol: class PsiMethod
location: class ParticleContainerManager
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors
The gradle source sets in intellij are as follows:
And when I CMD+Click the PsiMethod class, it shows the following JAR source:
This source is in the following parent folder:
How can I add this source into my gradle configuration? shouldn't it be autodetecting what my projects intellisense is tapping into?
Do I need to change something in the gradle.* files inside the project directory?
EDIT:
Using Yann's Answer:
I've added the following line to my build.gradle file:
intellij {
version '2019.2'
plugins 'java'
}
and it works!

Please make sure to add Java plugin as separate dependency. This is now necessary starting with 2019.2: https://blog.jetbrains.com/platform/2019/06/java-functionality-extracted-as-a-plugin/

Related

Compilation error in compiling Protobufs in Java using SBT build tool

I am using the Play framework (which uses SBT build tool) with Java where I need to consume a Protobuf. So I have xxx.proto file. I got binary protoc compiler and added to class path. so I see -
protoc --version
libprotoc 3.1.0
I have compiled the xxx.proto file using -
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/xxx.proto so it has generated xxx.java file.
Now when I am compiling this xxx.java file ( the project using sbt build tool)
[error] /my_project/app/helpers/xxx.java:7: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistryLite
[error] /my_project/app/helpers/xxx.java:11: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistry
[error] /my_project/app/helpers/xxx.java:6182: package com.google.protobuf.Descriptors does not exist
[error] com.google.protobuf.Descriptors.Descriptor
[error] /my_project/app/helpers/xxx.java:6185: package com.google.protobuf.GeneratedMessageV3 does not exist
[error] com.google.protobuf.GeneratedMessageV3.FieldAccessorTable`
I see in my installed library - com.google.protobuf jar is there.
My xxx.proto looks following -
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: xxx.proto
public final class xxx {
private xxx() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
......
Is there anything I have missed while generating the xxx.java file?
How should I fix these compilation error?
You need to make sure that you're using the exact same versions of protoc and libprotobuf.jar. From what you wrote, it sounds like you're using protoc version 3.1.0 but libprotobuf 2.5.0. You need to use libprotobuf 3.1.0 instead, otherwise you will get compile errors like the ones you quote.
Re-stating Kenton's answer with some more instructions:
In Intellij, click on External Libraries and find the jar for protobuf.
Check the version of protoc:
If they don't match, (as shown above) then you will get the compilation errors.
I've seen similar issue with maven after changing some field type in my proto schema and then building without doing a clean first. However, doing a clean and build fixed it every time.

Unable to access interface android

I am trying to access interface from other module in android studio where my project structure looks like this
projectRoot-folder
app-main-module //this is main app which i run
com.mypackage.files
MyClass.java
build.gradle
other-library-module //this is library module which i added
com.package.files
InterfaceFile.java
build.gradle
build.gradle
settings.gradle
I am implementing interface for MyClass.java and that is working fine with no compile errors,but when i press run it is giving the following error's can anyone explain why it is so?, any help is appreciated thank you.
Error:(5, 32) error: cannot find symbol class InterfaceFile
Error:(10, 52) error: cannot find symbol class InterfaceFile
Error:(11, 5) error: method does not override or implement a method from a supertype
Error:(16, 5) error: method does not override or implement a method from a supertype
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
You have to add in your file settings.gradle this lines:
include ':module2'
project(':module2').projectDir = new File(settingsDir, '../Project 2/Module2')
Then you have to add in your builde.gradle (Module: app) in the dependencies tree, this line:
compile project(':module2')
or go into the Project Structure > app > Dependencies, click on Add, choose 3 Module Dependencies and select your module

How do you add 'org.eclipse.core:org.eclipse.core.runtime:3.7.0' library to Intellij ant build

I can resolve the compile 'org.eclipse.core:org.eclipse.core.runtime:3.7.0' in gradle but instead using ant with the Intellij idea. I've included the org.eclipse.core.runtime-3.7.0.jar in my libs folder. I get the following errors.
Error:(22, 32) java: cannot find symbol
symbol: class IProgressMonitor
location: package org.eclipse.core.runtime
Error:(23, 32) java: cannot find symbol
symbol: class IStatus
location: package org.eclipse.core.runtime
Error:(24, 32) java: cannot find symbol
symbol: class Status
location: package org.eclipse.core.runtime
I'm attempting to build Android's UIAutomatorView, if I put the code into Android Studio it seems to resolve IProgressMonitor, etc. with compile 'org.eclipse.core:org.eclipse.core.runtime:3.7.0' in gradle.
Here's a screenshot of my project structure and source error.
Is there an easy way to add this to ant or just convert the project to gradle (detailed steps please).
You should have a build.xml file for your project. Look for the javac task (within the build file) that's being used to compile your code and make sure your jar is in the class path defined in there. If it's not, just copy it in.
Example taken from the below link:
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="YOUR_REQUIRED_JAR_FILE_HERE.jar"
debug="on"
/>
Source: https://ant.apache.org/manual/Tasks/javac.html

Getting compile error: package com.twilio.sdk does not exist

New-bee in java here.
I'm on an ubuntu 12.04 machine.
I am trying the Twilio API using java to make voice calls from an uiautomator test case and following the instructions provided at https://www.twilio.com/docs/java/install. I downloaded both twilio-java-sdk-3.4.2-with-dependencies.jar and twilio-java-sdk-3.4.2.jar from http://search.maven.org/#browse|1260284827 (pre-built).
I am using Twilio API in an uiautomator java project. I am able to build and run that uiautomator java project without implementing the Twilio API code. But if I try to use the Twilio API library, I get a compile time errors that it could not find the package.
Steps I'm doing:
1-> Open the java project in eclipse
2-> Add the Twilio java library twilio-java-sdk-3.4.2-with-dependencies.jar OR twilio-java-sdk-3.4.2.jar via BuildPath->Configure Build Path->Add External JARs.
I have following line of code to test if I can make the TwilioRestClient object. I have other test functions with the uiautomator and they work fine without this piece of code. Consider the following method in addition to other test methods.
test.java
//Assume all other required libraries are imported
import com.twilio.sdk.TwilioRestClient;
public class testClient extends UiAutomatorTestCase {
public void testMethodGetClient(){
try{
TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
log.info("client: " + client.getAccountSid());
}catch(Exception e){
log.info(e.toString());
}
}
}
I do not get any reference errors in my code before the comiple/build command. To believe that, if I do client. ,eclipse shows me all the methods available for the client object. So, can I assume here that my import was successful?
Then I go the terminal and execute below command to create the build.xml file:
ubuntu terminal
$> android create uitest-project -n JARNAME -t 1 -p <PATH-TO-PROJECT>
$> ant clean build
Buildfile: <PATH-TO-PROJECT>/build.xml
-check-env:
[checkenv] Android SDK Tools Revision 22.3.0
[checkenv] Installed at <ANDROID-SDK-PATH>
-pre-clean:
clean:
[delete] Deleting directory <PATH-TO-PROJECT>/bin
-check-env:
[checkenv] Android SDK Tools Revision 22.3.0
[checkenv] Installed at <ANDROID-SDK-PATH>
-build-setup:
[getbuildtools] Using latest Build Tools: 19.0.0
[echo] Resolving Build Target for <PACKAGE-NAME>...
[getuitarget] Project Target: Android 4.2.2
[getuitarget] API level: 17
[echo] ----------
[echo] Creating output directories if needed...
[mkdir] Created dir: <PATH-TO-PROJECT>/bin
[mkdir] Created dir: /<PATH-TO-PROJECT>/bin/classes
-pre-compile:
compile:
[javac] Compiling 33 source files to <PATH-TO-PROJECT>/bin/classes
[javac] <PATH-TO-PROJECT>/test.java:15: package com.twilio.sdk does not exist
[javac] import com.twilio.sdk.TwilioRestClient;
[javac] ^
[javac] <PATH-TO-PROJECT>/test.java:42: cannot find symbol
[javac] symbol : class TwilioRestClient
[javac] location: class <packagename>.Telephony
[javac] TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
[javac] ^
[javac] <PATH-TO-PROJECT>/test.java:42: cannot find symbol
[javac] symbol : class TwilioRestClient
[javac] location: class <packagename>.Telephony
[javac] TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
[javac] ^
[javac] 3 errors
BUILD FAILED
<ANDROID-SDK-PATH>/tools/ant/uibuild.xml:183: Compile failed; see the compiler error output for details.
Total time: 1 second
The above command would create the .jar if I did not have the testMethodGetClient method. So, I serached for the articals for package not found errors, but most of them suggesting to add the library either via 'Add External Jars' or 'provide the class path'. I tried both and I get the same error. So, I came here and posting it as a new question.
Any help is greatly appreciated.
Regards,
Rumit
The default location for third party jars is normally the libs folder at the base of your project, i.e. <PATH-TO-PROJECT>.
bgossit's answer did help me going to the right direction. However, that alone was not enough. So, answering my question by my self explaining a little extra step I had to do.
My first bottle neck is resolved, that is to compile the project successfully. However, I am still not able to run the project on an Android device. I am marking this comment as an answer as the question was for the complie error. I am puting a new question for the runtime error.
I found that the uiautomator's 'android create project' command creates a 'build.xml' which internally calls '<ANDROID_SDK>/sdk/tools/ant/uibuild.xml' to build the project and create the final jar. By default, the 'compile' target in this 'uibuild.xml' does not have a 'classpath' inlcuded for the jar. This seems to be the bug with Android as mentioned on couple of other StackOverflow questions/answers.
Solution that worked for me:
Create the 'libs' directory at the same level as your 'src' directory and put your external jars into 'libs' directory. Note: This is just a good practice. Ideally you can give any name to the 'libs' directory and put it wherever you want.
Add the 'classpath' attribute to the 'compile' target in <ANDROID_SDK>/sdk/tools/ant/uibuild.xml.
<classpath>
<fileset dir="${jar.libs.dir}" includes="*.jar" /> <!-- path to 'libs' -->
</classpath>
PLEASE NOTE: Although, above solution worked to compile and build my project, I'm still not able to run it on the Android device as the Android device would not have Twilio API jar. I am looking for the ways to build the final project jar with Twilio jar/classes. java.lang.ClassNotFoundException for package com.twilio.sdk
Regards,
Rumit

Problems using protobufs with java and scala

I have a file xxx.proto. I downloaded the protobuf compiler and installed it. Then I issued this command
protoc --java_out=./ xxx.proto
and it generated my xxx.java
Now I want to compile this file into a class file which I can use with Scala.
javac xxx.java
Which gives me this error
xxx.java:7: package com.google.protobuf does not exist
com.google.protobuf.ExtensionRegistry registry) {
^
xxx.java:12450: package com.google.protobuf.Descriptors does not exist
private static com.google.protobuf.Descriptors.Descriptor
^
xxx.java:12453: package com.google.protobuf.GeneratedMessage does not exist
com.google.protobuf.GeneratedMessage.FieldAccessorTable
...
...
...
100 errors
Now I guessed, it doesnt have the package.
So I copied the class files of package com.google.protobuf into the same folder where xxx.java exists. Note - I didnt compile this package. I downloaded the jar from another extension which had the jar files. So I extracted them. Now my current path where xxx.java resides has com/google/protobuf/ *.class of protobuf library.
I issued the javac command again.
This time I got a different set of errors -
xxx.java:10: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:215: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:608: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:1017: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
..... 100 errors
I even tried to compile the source files which came with google protobufs. The generated java classes are giving the same errors.
Any ideas what to do ??
Answer
Okay. Thanks everyone.
The main problem is that protocol buffers compiler package from google doesnt by default create the java library. I assumed that it does and installs it. It actually does if you are running Maven. But i didnt have maven
So i compiled the code in /java/src and used the jar.
^
When compiling, you need to have protobuf lib on your classpath. All those missing packages and classes are from protobuf lib.
Find protobuf jar and use
javac -cp path/to/protobuf.jar xxx.java
You may need to use version 2.4.1 (or 2.4+, at least) of the protobuf kit, including making sure that you update protoc (the protobuf compiler) and recompile your proto definition using the new protoc. (In other words, everything has to be the same version:
the protobuf-vn.n.n.jar file;
the protoc compiler; and
the output of compiling your .proto files with protoc.
One I got everything synched, I began to move forward with a Clojure project I'm looking at. You may be encountering the same version skew problem.
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto
you can download protoc.exe (new release) from>>.
https://code.google.com/p/protobuf/downloads/detail?name=protoc-2.5.0-win32.zip&can=2&q=
in your *.proto file you correctly config
option java_package = "com.example.package";
option java_outer_classname = "class name";
One can install the protobuf jar file using the ubuntu a
apt-get install libprotobuf-java
This will copy the protobuf-java-2.4.1.jar under /usr/share/java/
Hope this helps

Categories