Trying to import a class I made in Dr. Java. I made a simple class called QuestionObject which has String questionString and an array for answers and then an int which corresponds to the correct answer and corresponding getters, setters, and constructors. Compiles fine. In my other class file, the one with my main method and such called Game, when I try import QuestionObject; I get a 'cannot resolve' error in the compiler. I saved the class in the same folder as Game.java. Doing everything in OS X not using command line.
Help please! There's probably a simple answer, I just can't find it!
Don't import the class if both classes are in the same directory and in the default package!
Try removing the import statement.
Try setting the package of both classes. And make sure that both classes are in the class path. I am not familiar with Dr Java but with the command line compiler javac *.java in the directory with the classes should pickup both classes.
Related
Trying to compile a multi file package. Needing to compile via: javac mainfile.java while also compiling all the other (about 4 other .java files) at the same time through the one file? I have tried using statements like extend and import package.* Any help would be appreciated.
I have used different compiling methods and arguments but trying to do it just by only inserting javac mainfile.java and java mainfile
Summary: Trying to compile multiple java files at once, through java compiling a single file.
My current code for the main file:
package mypackage;
import mypackage.*;
public class mainfile{
public static void main(String[] args) {
Myfile.main(args) //run main from other file
}
}
Edit: Sorry for the lack of information, when compiling, the compiler returns:
MainFile.java:15: error: cannot find symbol
Myfile.main(args);
^
symbol: variable Myfile
location: class mainfile
1 error
javac as a tool does not do what you want. But, that's why other tools exist.
What you're asking for boils down to 'I want a build system'. The vast majority of java projects use maven or gradle.
It would seem like this works fine:
javac -sourcepath src src/mainfile.java
but you'd be deceived. That will merely compile all source files that are directly referenced by the code in mainfile.java, but there are many other ways to refer to code, such as SPI, reflection, XML config files. The vast majority of java projects will end up using some construct that ends up 'breaking' the -sourcepath "trick" sooner rather than later, which is presumably why all java projects use a build system instead of relying on -sourcepath.
Note that all source files act as if they have:
import java.lang.*;
import yourownpackage.*;
at the top, whether you write this out or not. And, import is java-ese for 'alias'. import foo.bar.Baz; means: Whenever 'Baz' appears in this file as a type, assume I meant to write 'foo.bar.Baz', and that is all: import foo.bar.Baz does not run any code that is in the Baz class whatsoever. If you don't use Baz, then the class file produced doesn't mention Baz at all. This will also not cause javac to then compile your entire directory.
I have this program with 3 source files, being pretty new to java I recently learned that I should package these. So I went and did that, I already knew a little about packaging. However, after trying to run the class file with the main method, java could not find/load the main method.
After a while, I finally discovered that the cause was the package line. When the package line is there, the error appears, when the package line isn't there (or is commented out), the program runs fine.
package PeriodicTable;
class PeriodicTable {
public static void main (String[] args) {
//Lines of code
}
}
According to various tutorials and the java doc, all you need to do is put the package line, the package name and a semi-colon at the end. Google searching the error (with package as the cause) did not help me.
I've tried changing the package name, so it was not the same as the class name, this did not work.
What am I using?
Notepad
Command Prompt
Java 8
As for my question... Why is the package line preventing java from finding/loading the main method? How do I fix this?
Go to the package directory, in your case it is PeriodicTable. Run following commands
$ javac -cp . PeriodicTable/PeriodicTable.java
$ java -cp . PeriodicTable.PeriodicTable
Hello
Following link may help you.
Yeah, I've noticed this as well in the past. I just remove the package, since I'm usually calling just a couple programs from the same package anyway at most. :) My mantra is, if it works, take yes for an answer and move on...l
I have the following directory structure :
MessManagement is the parent directory.
Under that i have 3 directories :
student , messconvener,messmanager
The student directory contains the Student.java and Student.class files. The messconvener contains the MessConvener.java this requires the Student class as MessConvener is extended from Student itself.
How should i do the packaging of the classes....??
What i have tried so far.
Code :
This is Student.java
package MessManagement;
import java.sql.*;
public class Student
{
}
This is MessConvener.java
package MessManagement;
import MessManagement.student.Student;
public class MessConvener extends Student
{
}
But this does not seem to work.Error Meesage :
MessConvener.java:2: error: package MessManagement.student does not exist
import MessManagement.student.Student;
^
MessConvener.java:3: error: cannot find symbol
public class MessConvener extends Student
^
symbol: class Student
2 errors
The error you're getting makes sense. The Student class is located in MessManagement package not MessManagement.student package.
So either remove your import of Student in MessManagement or change the package name in Student to MessManagement.student.
For me, I got this error for a different reason and this was in a maven project. It began to occur after I made major changes to the classes and copied in a lot of new classes. There were no conflicting package names as in your case.
The solution was to do mvn clean.
After this, I didn't get that error anymore.
There are two possible reasons why this happens
Is the root of your directories included in the classpath? You need to specify when starting a java program. See the documentation on how to do that or this variant for unix.
Are your classes public? If you forget the public modifier, classes will have package visibility and cannot be accessed from other packages.
Oh well, nobody expects the spanish inquisition ... check your spelling carefully, including capitals.
I recently had this issue and it was because I had a class file in the same directory as the java file that I was compiling. You need to delete all .class files in that directory.
I deleted the out folder and the problem was solved
For me it happened after updating java to jdk_16.
it was solved after I changed the "misc.xml" file inside ".idea" folder.
changed languageLevel="JDK_15" to languageLevel="JDK_16".
In Intellij, try
Go to File
Invalidate Caches/Restart
You can choose only Invalidate and restart
See this answer
Well I guess I am literally 8 years late, but today I had this problem and I managed to solve it just re-writing the state of the class. Seems that if you comitted your project, pulling it back could make you have that problem, but writing again the "public" state of your class worked for me. I Hope it works for you guys. chrss.
I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.
How do I create a Java package for different files? I have tried
the following. What have I done wrong? And what is the
right procedure?
The first file is:
package dil;
public class Hello
{
Support sp=new Support();
int i=sp.tin();
public void man()
{
System.out.println(i);
}
}
The second file is:
package dil;
class Support
{
public int tin()
{
return 3;
}
}
Now while I compile hello.java it shows these errors:
Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
^
Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
^
Where is the problem and how can I put both
these files in a package?
The files are in c:\src.
Assuming UNIX / Linux pathnames, a UNIX shell, etc, you need the following file structure:
/some/where/dil
/some/where/dil/hello.java
/some/where/dil/Support.java
Then set $CLASSPATH to /some/where, and compile using the commands
cd /some/where
javac dil/*.java
and run using
java dil.hello
Alternatively, you can tell java and javac what classpath to use with the -cp command line option.
You should also fix the following errors in the code:
Change the name of the "hello" class to "Hello", and rename the source file to match. Strictly speaking this is not an error, but it is a gross violation of the standard for naming Java classes.
You declare a member as "ten" but refer to it as "tin". Fix one or the other.
The entry point method in the "hello" class should be called "main" not "man", and should have a signature public static void main(String[] arg). If you don't fix these the code will compile, but the java command won't find the entry point and will fail.
Although the Support class is not public, that would not be a problem as both classes share the same package. My guess would be that you did not put both source files into a directory according to their packagename and call the javac compiler from the current directory where hello.java resides.
If a class is in package a.b this means the project structure should contain a folder ./a/b containing yourclass.java.
In your case, try to create a folder named ./dil, put your source files in it and call javac from its parent folder.
See Creating and Using Packages in Sun's Java Tutorials to learn all the details of using packages in Java.
I've spotted some things you have to check:
class hello starts with a lower case
class hello calls for sp.ten() instead of sp.tin()
Support isn't public. Make it public and try again.
I suggest you try using one of the free IDEs like Netbeans, Eclipse or IntelliJ CE. This will help you start coding rather than setting everything up the hard way.
BTW: These IDEs have quick fixes for most common problems so they not only give you the error but give you options to fix them and do it for you.