We have this project setup for eclipse that I'm now trying to import into IntelliJ.
I've tried using the eclipse import in IntelliJ but that fails, I suspect it's because the person who set up the project decided there shouldn't be a src-folder and instead named it after our course, tddc17...
The structure looks like this:
- project
- lib (contains two jars)
- tddc17
- MyVacuumAgent.java
Now I've set up the jars as modules in IntelliJ and when I configure the run I can find the entry point so that's all good. The project also builds as it should but the problem is when I try to run it.
In one of the jars it tries to find "tddc17.MyVacuumAgent.java" which it then can't find.
I can't edit the jar so I can't change that, so what I need is a way to set it up so that it can be found. When building there's an out directory created with the structure:
- out
- production
- lab1 (name of the project in IntelliJ)
Could that be the issue?
Figured out the problem.
since it looked for tddc17.MyVacuumAgent.java it was expecting that to be part of a package. Which it wasn't. So I added package tddc17 at the top of the file. THis gave me an error but using alt + enter to let IntelliJ fix it created yet another tddc17 folder inside the existing one and now it all works.
Related
I'm new to the whole programming stuff but here's my problem:
I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.
Currently, I am not able to implement any test methods because Eclipse tells me on the line
import static org.junit.jupiter.api.Assertions.*;
the error message
The type org.junit.jupiter.api.Assertions is not accessible.
Error I get in the IDE:
I tried the following:
Reinstalling Eclipse, using a fresh workplace.
Adding the JUnit to Build path
Nothing helped.
It worked and works in older projects just fine.
Here is how the Package Explorer looks:
What am I missing?
You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)
I guess by asking this I might sound a bit illegible, but I'm still unsure as to how to approach the problem.
In my spring project (not really my, work stuff) I've got some groovy scripts which are initially treated as resources, yet in reality they are rather the "source code" which is compiled not during the gradle assembly of the project but during the runtime by the application itself. And everything's fine with that.
The problem is that the IDE doesn't treat the groovy file properly. Dumb example to somehow describe what I mean:
import myproject.example.blabla
import groovy.transform.CompileStatic
#CompileStatic
class SomeClass1 implements SomeClass2 {
private final SomeClass2 someName1
SomeClass1() {
someName1 = new something
}
#Override
String getSmth() {
return someName1.getSmth()
}
}
The problems:
when I make "command + left_click" on SomeClass2, it says Cannot find declaration to go to, but when I press "command + O" it finds the file because it actually exists
.getSmth() is red, because Cannot resolve symbol
So it seems that I need to somehow show the dependencies via gradle to IDE only. Like, somehow specify the dependencies explicitly for IntelliJ IDEA so that it would understand that it is a source code as well and stop underlining everything with red.
Such files must be located in the module's Source Root directory for the IDE to recognize them as sources and so that navigation would also work.
In a Gradle-based project IDE configures Source Roots automatically based on the Gradle's Source Sets configuration. For each Gradle source set IDE creates a module with one Source Root directory.
So you must configure Gradle to create source set for the directories where these files are located: add them into default sources sets or create a custom source set for them.
I know I am asking the very popular question. But I can not find the solution to the problem. I have a sandbox to which I added a code of the unit test MulticurveBuildingDiscountingDiscountAUDTest.java file and commented it.
Then I added the main method and I could successfully run the program (print something in a console).
Finally, I uncommented the code of the MulticurveBuildingDiscountingDiscountAUDTest.java file and I saw the following error:
The import com.opengamma.analytics.financial.instrument.index.GeneratorSwapFixedONMaster cannot be resolved.
And further in the code:
GeneratorSwapFixedONMaster cannot be resolved
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path. I believe the problem is with a build path options and specially with classes like GeneratorSwapFixedONMaster which were created specially for tests. I have been playing around with cleaning, rebuilding projects, reinstalling and as a result updating the JRE. I have visited these Import *** cannot be resolved [duplicate] and these Eclipse error: “The import XXX cannot be resolved” questions.
Do you know what shall I do to cure the following error?
I have many problems with other imports from the original MulticurveBuildingDiscountingDiscountAUDTest.java file as well.
Update: #1 is a location of my file. #2 is the location of classes this project uses. The MulticurveBuildingDiscountingDiscountAUDTest.java file is taken from the src/test/java
Update 2: one may see that in Libraries I have included all the dependencies I might need (at least I do not know what else to add). The Maven Dependencies contains the hole og-analytics package:
You included the source (src) folder og-analytics/src/main/java which contains the *.java files instead of the classes (bin or classes) folder with the *.class files (in your case, probably og-analytics/target/classes).
But instead using Add Class Folder... you should add the project og-analytics in the tab Projects. Or even better, in the Maven pom.xml file add the dependency to the project og-analytics like you did for og-util.
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path.
Perfectly explains your problem. In order to import any class, you must either have the source in your build path, or some directory that contains a compiled version of that class. It is that simply.
The answer is: get clear on your project setup. If you intend to use classes from somewhere, you have to make them available somehow. Otherwise it will not work. In your case: if your tests want to make use a certain thing - then you should probably add that "thing" to your test project (you should avoid putting test-only stuff to your "product" projects).
That is all there is to this.
Here are the steps I have followed so far, with no luck. I am extremely new to Java projects so I suspect I may be missing something obvious.
Using Eclipse, I have created a simple Java project called TestSDK, created within that a package called com.test.testsdk, and within that the following class:
package com.test.testsdk;
public class TestClass {
public void TestMethod() {
}
}
This compiles without errors or warnings.
I then export this as a JAR file (TestSDK.jar) using Eclipse and the standard export options (export generated class files and resources, compress the contents of the JAR, generate manifest file). I have tried both sealing and not sealing the JAR which makes no difference.
I then create a new Android application project from File->New->Project in the Wizards list. This compiles and runs without warnings or errors on both the Android emulator and my test device (I get the hello world message).
I then add a reference to my TestSDK.jar file (using a variety of different methods as I will expand on shortly), import it into the main (and only) Android activity, and try to instantiate my TestClass and call TestMethod on it, like so:
package com.apptest.mobilesdktestapp;
import android.app.Activity;
import android.os.Bundle;
import com.test.testsdk.TestClass;
public class MobileSDKTestAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestClass test = new TestClass();
test.TestMethod();
}
}
This compiles fine without warnings or errors. When trying to run it on the emulator or the device, however, I get the following error in my LogCat window:
AndroidRuntime Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime java.lang.NoClassDefFoundError: com.test.testsdk.TestClass
Searching the web for the NoClassDefFoundError results in a lot of suggestions on how to import the JAR file such that the class path is correct. As a result, I have tried all of the following methods of importing the JAR file:
"Add External JARs..." from the Libraries tab of Java Build Path in the project properties, followed by checking (or not checking, I tried both) the JAR in the Order and Export tab. Also tried moving the JAR to the top of the Order and Export list, which made no difference.
Creating a "libs" folder in the project, and adding the JAR there. I confirmed that the JAR is then also added to the "Android Dependencies" thing in the project list. Also tried right-clicking the JAR file and selecting Build Path->Add to Build Path which made no difference.
Moving the JAR into my Android Application project directory and doing "Add JARs..." instead of external JARs as in step 1, also all permutations of exporting or not and moving it to the top of the order list or not.
I have subsequently downloaded other 3rd party SDKs that are packaged as JAR files and included those in the very same Android application project, and those have all worked fine using any of the 3 methods above (I am able to instantiate classes from those SDKs and use them without error), which leads me to believe I am missing something or doing something wrong in my TestSDK project and/or class which is preventing it from being used in the Android Application project.
As I said, I am very new to Java, so I'm hoping it's something simple that I've overlooked. Any help or guidance would be appreciated.
If you are on R17 or higher version of the Android tools and ADT in Eclipse, then the first sentence of #2 is the correct answer; everything else listed in your question is unnecessary at best or harmful at worst.
I would recommend that you create a clean project, create the test activity, create the libs/ folder, copy the JAR into the libs/ folder, code to the JAR's API, compile, and run. If that works, then your original project still has stuff lingering around from your previous efforts that is causing you grief. If it fails, then something fairly strange is going on. The JAR itself is presumably fine, otherwise you would get compile errors.
I think I figured out what the issue was (or at least how to fix the issue, I'm still not 100% sure what I did)--
When I created the original TestSDK java project, I let it target the default JRE in the project creation dialog (jre7).
Checking the project properties of a new Android Application project, the Java Compiler section has "Compiler compliance level" set to 1.5. So, I tried recreating my TestSDK project again, but told it to use J2SE-1.5 as the execution environment instead of the defalut jre7.
After doing this, exporting the JAR and importing it to the Android project's libs directory, I am now able to instantiate the TestSDK classes and use them just fine without the NoClassDefFoundError exception.
Best guess is that the Android application was being compiled against an older version of the JRE than my TestSDK class (which I believe was targeting JavaSE-1.7), causing the issues. Matching the two versions up has solved it.
I am trying to import:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I am being told these do not exist?
I downloaded:
httpclient-4.0.1.jar and httpmime-4.0.1.jar
... and placed these in the same folder as my .java files that are trying to use httpclient.
Any ideas?
I still cannot get it to work... Within the folder "Libraries" I have: apache-mime4j0.6.jar commons-codec-1.3.jar commons-logging-1.1.1.jar httpclient-4.0.1.jar httpcore-4.0.1.jar httpmime-4.0.1.jar For the java file properties it has: compile classpath runtime classpath boot classpath In each of those, it seems to refer to the jars I have imported. Still getting does not exist. :-(
I have tried to do this in Eclipse too and now those files appear in "Referenced libraries" however it still doesn't work. lol
The two jars you have mentioned need to be placed in the classpath of the project in Netbeans, not in the source directory.
In my Netbeans 6.7.1 on Mac, in the Prjects tab, you cna right click on the project and select Properties. That will bring up the project properties dialog. In there, choose the libraries item from the tree on the left. From there, choose the Add Jar/Folder in the Compile view. To add the jar to your project, use the chooser to locate it and then select it.
EDIT:
I have just downloaded the HTTPClient package and I think I see the problem:
in 4.0.1, the package structure is not as you have it defined. Instead of:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
use:
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.params.HttpMethodParams;
In Eclipse, press Ctrl + Shift + O to organize your imports. This will look for all unknown classes on the classpath and try to import them. You can also place your cursor on a class name and press Ctrl + Shift + M to attempt to import that single class. This is sometimes helpful for class name collision (i.e. if two packages have a HttpClient class, you can click on the desired class).
If the jars are in Referenced Libraries, then they should be on your classpath. You can verify this by right clicking the project and selecting something like Build Path > Configure Build Path, then click the libraries tab.
Also, you probably have build automatically selected by default, but if you don't, you'll need to build your project. You may also want to attempt to clear the build path and re-build it. I've seen my Eclipse get out of synch a few times and this fixed it, albeit somewhat of a fluke.
If you're using Maven, this sort of thing can sometimes occur if you have an incorrect dependency scope (i.e. runtime, or test vs. compile).
For what it's worth, unless you're utilizing the entire package, there is no reason to import an entire package's contents (i.e. import package.*).
It seems that HttpClient has changed his sintaxis from 3 to 4 version... I had same problems that all of you trying to import packages until I found this example:
http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/
This is sample is Android oriented but works on any Java Application!!! Im using netbeans 6.9.1, httpclient-4.1.1.jar, commons-codec-1.4.jar and commons-logging-1.1.1.jar
Hope you can solve your problems!!!
Had the same problem and i managed to get the solution. Here it is:
1) Download the org.apache.commons.httpclient.jar.zip file from
http://www.java2s.com/Code/Jar/o/Downloadorgapachecommonshttpclientjar.htm
and save it anywhere on your computer.
2) Right click on your NetBeans project and select Properties
3) On Project Properties Categories, select Libraries
4) Click the Add JAR/Folder button
5) Now browse to the file location where you saved your downloaded org.apache....jar.zip
file and click open button.
6) Now the file has been installed, click OK and you are done.
Note that this might require you to restart your netbeans IDE.