Import existing .java files into IntelliJ? - java

I'm trying to import .java files into a new IntelliJ project and am really not having any luck (which includes Google, Stack, IntelliJ docs) finding out how to do so.
I create a new project, the Java SDK is configured correctly and everything works fine. The Main class of the IntelliJ project is located in a package called 'com.company', which is located in a 'src' directory. I try adding my files to the package, no go. I try dumping my files into the 'src' directory, can't instantiate any objects.
I've tried import statements to no avail.
When I try to instantiate an object of any class, a red line appears under the class name with 'Cannot resolve myClass'. myClass = all of my classes
Can somebody tell me what the missing piece is?

Related

IntelliJ can't resolve symbol despite adding library

I am creating an external library to share code between my client/server programs. IntelliJ can't seem to import this jar file and I can't understand why, I followed all the advice I've found online.
I added the jar to my lib folder and added it as a library in my main module (it's called 'common':
This is the error (repeated on each instance of me using User or Packet). On import statements it says "package common does not exist":
I've tried invalidating caches and that didn't seem to work either. I tried this same import on a new project just to see if I was doing anything wrong and it worked perfectly on that new project so I'm not sure what's going wrong here.
EDIT:
I've noticed that this error only happens in files that are in packages within the src folder. So basically, Main can find the jar because it is only in the src folder, but RegistrationController can't because it's in src/communication.
The problem was that the shared code I was making was not in a package, just a src folder. Because of that, the code that was in my default package (src) could access the classes, but not the code in packages. To fix this, I put the shared classes in a package (com), exported them as a jar, and imported that into my project. Now, my packages can do com.Packet or com.User and get the files successfully.

Java import error - org.jdom

I am attempting to edit code from an old developer.
I know it is bad practice but what she said I had to do was create a dummy project and then copy paste the packages in question modify what I need to modify then compile and copy the classes that I changed into the directory where the classes in use are stored.
So I created the dummy project and copied the packages/files into the source folder of the dummy project but I keep getting a "package org.jdom does not exist" error.
The error appears on the lines:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
When I copied the folders/files into the source folder I copied the entire "org" directory.
You can see from my screenshot that the package is in the Project Explorer
I have several things to resolve the issue but none seemed to work.
I tried adding the "org" directory into the classpath environment variables.
I tried adding the "org" directory as a library to the project.
I have tried clearing the NetBeans cache after doing both of these but none of these 3 things worked.
Below is a screenshot of the projects src folder in file explorer
Can someone explain to me what I need to do to get this issue resolved?
I was adding the wrong files to the libraries section of my project.
I was adding the source for jdom when i should have been adding the jdom .jar file.
Thanks for the help guys.
install jdom at your library folder, there is a new version of jdom try to download it and install it in your project directory.

How do I know the path for Java import?

I'm trying to learn Java ( I know php ) but I don't know what path the included classes have. For example, I create a new directory in Eclipse (in the package) and drag there a class from other project. When I try to import it, it cannot find the class. Even if I don't have any dirs and the class is directly in the package, using import package.classname doesn't work...
I must be missing something but googling doesn't show me any replies.
How do I get the class path? Is it somewhere in the properties?
Java has the concept of a classpath: a path where all classes should be found.
You can get the existing classpath with this code:
System.getProperty("java.class.path")
If you run java from the command line then you have to set your own classpath with your classes.
From the classpath, use the package to find the class. For example, if the classpath is ".", which is the current folder, and you have a class called A, which is in the package com.yourcompany, then you will find the class under ./com/yourcompany/A.class
On the example you gave, go to the terminal and look for the "bin" folder and you will see all classes. However, if you want to add a new class from another project to your project, then there are simpler ways. You can simply open your build path in Eclipse and add the class from the other project onto your project.
Another way is to create a jar from the other project and add the jar to your project.
In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.

External class folder not exportable?

Eclipse, Android project. I have a library project (mylib) and an app that references it (myapp). I've added to the build path of mylib a folder with a bunch of compiled Java classes (com.foo.*), checked it as exported. Added a call to one of those classes to the main activity. There are no build errors. But when I run myapp and try to call a method from com.foo, I get a "class not found" exception.
When I package the same class folder into a jar file and reference/export that in Eclipse, the app works.
What am I doing wrong? Are class folders not subject to export somehow?
EDIT: here's my thinking. The Java Build Path window treats JAR's and class folders uniformly. Any why shouldn't it - a JAR is, for the most part, just a class folder zipped up. But then there's this discrepancy of behavior when passing classes on to other projects. Unexpected...
You need to make it an android library and import it as such in the android panel

Access external class files in java using eclipse

I have a class file to handle the GUI which is SampleGUI.class. I want to use that into my java project. I have created a folder named ClassFiles in root directory. I put the SampleGUI.class inside that folder. I went to project properties ->Libraries->Add Class Folder. Then i tried to call that class inside my new class created inside the project like
new SampleGUI ( length, width) ;. But it gives me the error "SampleGUI cannot be resolved to a type". Do i need to import anything? How do i procees with this situation?
I have create a Jar file using the command
jar cf jar-file input-file(s)
And I imported those jar files into my project and it all worked fine.
If SampleGUI is in a different package, you'll need to import it.
e.g.
import ClassFiles.SampleGUI;
where ClassFiles is the package name.
If SampleGUI is your own, and you have the .java source file, you can add SampleGUI to the ClassFiles package, if it isn't already, by adding the following line to the top of SampleGUI:
package ClassFiles;

Categories