How to move one folder back in java while importing packages - java

I have one Folder by the name of "java" which has two folders "Search","Demo".
in "Search" folder I have so many class including "BinarySearch.java" and in "Demo" folder I have class "Demo.java" I want to import "BinarySearch.java" class in the "Demo.java" class.
So I tried this:
First in "BinarySearch.java" I put this line of code package Search;
in "Demo.java" I put this line of code import Search.BinarySearch.java
But this solution only works only when I move the "Demo.java" file in "java" folder. but I need to import "BinarySearch.java" without moving my file into "java" folder.
See the error If don't move the file into "java" folder
So I need to move back one folder while importing.

Use this:
In Demo/Demo.java
package Demo;
import Search.BinarySearch;
public class Demo {
.
.
.
}
In Search/BinarySearch.java
package Search;
public class BinarySearch {
.
.
.
}
It is always a good practice to use lower case for your package names. Example: demo or search.

The first line of Demo/Demo.java must be "package Demo".
The same as the first line of Search/BinarySearch.java is "package Search".

Related

Changing name package to previous version

Strange problem, my package was:
com.myname.myappname
after change is:
com.myname.myappname.abc
and now I cant back to previous name because when I'm going to rename I can change only end of package "abc" and it is not possible to leave this empty field.
so how to set my package to first edition ?
you can do it manually by moving all the files in abc package to parent package & then changing package path in all the files, time consuming but it will work.

Eclipse java file not found until edited and saved

I have the following file structure in my eclipse workspace
core
src
com.mygame.game (package)
Ability.java
Game.java
...
Allies-Green.java
Ability.java and Game.java were created using the 'right-click on package->New->Class' method. Allies-Green.java, however, was created using the 'right-click on package->New->File' method. Each time I load my workspace, all code that references Allies-Green.java is marked as an error. If I open Allies-Green.java though, and edit one character and resave it, the problem goes away. Does anyone know what could be causing this issue? Here is Allies-Green.java relevant code for reference:
package com.mygame.game;
import java.util.ArrayList;
import ...
//only class in file
class AllyGreen extends Ally {
...
}

Can't use jar library in my java file

Sorry for this noobie question, I'm new to Java, and instead of using IDE, i want to using command line to learn what's running under the hood
I'm following the Getting Started guild on MigLayout
#MigWindow.java
public class MigWindow {
public static void main(){
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());// a simple line to make sure the library jar import correctly
}
}
and compile with these command:
javac -cp ./MigLayout.jar MigWindow.java
and I got a error:
MigWindow.java:3: cannot find symbol
symbol : class MigLayout
location: class MigWindow
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());
^
1 error
It seems the jar library doesn't import correctly, any idea?
~
Make sure you add the import for MigLayout
import net.miginfocom.swing.MigLayout;
It may sound obvious, but make sure MigLayout.jar the current directory when calling javac here and that your JAR file has not been corrupted.
Update:
To check that your JAR file does contain the class you can do:
jar tvf MigLayout.jar
and check for the MigLayout class. Failing to find the class you can download the correct one from here.
You are missing an import statement in your Source File. The compiler does not know where 'MigLayout' is coming from.
Add at the top of your file, but below of your package statement (if any) an import, e.g.
import package.MigLayout;
This tells the compiler what to import from the given class path. You will need to replace package with the correct package.

Protocol Buffer: How to import?

I have 2 .proto files :
First file:
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
Second file:
package com.test.model;
message CustomObj {
required string smth=1;
}
The issue here is that "CustomObj" is said to be "unresolved reference" .
Thus, I've tried to import the second file into first file:
import "com/test/model/firstFile.proto"
package com.test.model;
message ProtoModel {
required CustomObj custom=1;
}
I still get the same issue !!
The import statement is the folder relative to the place where you invoke protoc.
It looks like you have treated it as relative to the package instead.
e.g. if (like me) you store both files in src/main/resources, you'd invoke protoc as follows:
protoc src/main/resources/firstFile.proto src/main/resources/secondFile.proto --java_out=src/generated/java
and your import statement would be import "src/main/resources/firstFile.proto"
If you want to store the files in subfolders according to package name, then you just add this accordingly, after the top-level foldername.
HTH

Problems when trying to compile a Hello World with Eclipse

I'm having problems when trying to compile the following code:
I first tried compiling with this code:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
it works as it should. Now, if I try to replace the class name for any other name, it won't work anymore, as it seems to always look after HelloWorldApp. I made sure the file is being saved and so, I even reopened Eclipse. Still the same error. Maybe this is a common problem, with a small work-around?
Thanks
edit: I see what you guys mean, but why does it work when I have the file name as "Main.java" and a class name of "HelloWorldApp" ?
You need to rename your .java file to match the class name.
Eclipse will rename your .java file to match your class name automatically if you use its refactoring support. Right click on the class name, hover over Refactor, and select the Rename option. Now when you rename your class, Eclipse will automatically rename your .java file to match.
This is how Java works. Class must have the same name as the file. So the filename for the class MyApp, must be MyApp.java
Rename main as HelloWorldApp so that your app becomes HelloWorldApp.java.
When changing the name of your class use the REFACTOR option - If you try to edit the name manually yourself the Eclipse Project loses track of your objects.

Categories

Resources