Custom Package Importing Issues - java

I am trying to create custom packages and i put in my other Program But when i import this Package that it will gives me error.
This is Custom Package File :
package Collage.Softwere;
public class IT{
public void meth1(){
System.out.println("This is method 1...");
}
public static void main(String[] args){
IT obj = new IT();
obj.meth1();
}
}
This is Test file where I Import This Package :
import Collage.Softwere.IT;
public class Test{
public static void main(String[] args){
IT obj = new IT();
obj.meth1();
}
}
I get This error :
error: meth1() is not public in IT; cannot be accessed from outside package
obj.meth1();
^1 error
MY directory set up is :
-->Package (Folder Name)
--> College (Root Directory)
-> Software (Sub Directory)
* IT.class
* CE.class
-> Hardware (Sub Directory)
* ME.class
* AME.class

You have a typo in the imports.
Collage.Softwere -> Collage.Software
Collage.Hardwere.ME -> Collage.Hardware.ME
if it isn't that typo, you need to make sure in your dependency management that the package is added as a dependency in the right pom.xml (if you're using maven).

Related

How to read the file entry with same name in multiple library jars with spring boot?

I have a spring application with multiple dependent libraries that have the inner properties/xml that I want to read.
test-app.jar
dep1.jar
dep2.jar
dep3.jar
...
Each of the dep1/2/3 jars have the file called META-INF/config.properties which contains the files to further read within that dependent jars.
I tried the ResourceUtils.getURL("classpath:/META-INF/config.properties"), but it always reads from the first dependent file.
How can I read from each jars that contains the same name?
I found this solution after searching:
final Enumeration<URL> resEnum =
MyClass.class.getClassLoader()
.getResources("META-INF/config.properties");
final List<URL> resources =
Collections.list(resEnum);
//Copied from riptutorial.com
Refs:
https://riptutorial.com/java/example/19290/loading-same-name-resource-from-multiple-jars
https://stackoverflow.com/a/6730897
Updated solution:
package abc;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
public class MultipleClasspathEntries {
public static void main(String[] args) throws IOException {
final Enumeration<URL> resEnum =
MultipleClasspathEntries.class.getClassLoader()
.getResources("META-INF/MANIFEST.MF");
while (resEnum.hasMoreElements()) {
System.out.println(resEnum.nextElement());
}
}
}
//Copied from riptutorial.com
$JAVA_HOME/bin/javac abc/*.java
$JAVA_HOME/bin/java -cp /c/so-67847004/commons-lang-2.4.jar:/c/so-67847004/commons-collections-3.2.1.jar:. abc.MultipleClasspathEntries
Output:
jar:file:/C:/so-67847004/commons-lang-2.4.jar!/META-INF/MANIFEST.MF
jar:file:/C:/so-67847004/commons-collections-3.2.1.jar!/META-INF/MANIFEST.MF

Debug Imported Files in VS Code

I have recently set up the Java extension for VS Code.I have a folder that contains two files Main.java and Person.java. Main.java calls Person.java. When I set a breakpoint in Main.java, everything works as normal.However, when I set a breakpoint in Person.java, it just skips over it.
Are there any workarounds in this issue?
Main.java file
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Person person = new Person();
person.speak();
}
}
Person.java file
public class Person {
public void speak(){
System.out.println("Speak!");
//breakpoint on this line right here gets skipped
System.out.println("Speak!");
}
}
Run -> Start Debugging, it stops at the breakpoint:

Custom library gives "no package found"

I decided to make a custom .jar library just for the hell of it.
It's insanely basic, it's package name is "add" and looks like this:
public class addtogether{
public void addtogether(int a, int b, int c){
c=a+b
}}
So I try importing it in a file, by doing this:
public class test{
import add.*;
public static void main (String[] args){
int x;
add(4,5,x);
System.out.println(x);
}
}
and surprise surprise! No package found.
I did the whole dance of adding it to the IDE in the preferences section, but it still doesn't work.
help please.
A very naive step by step implementation for using a custom jar:
Create a java project with a utility class containing your functionality. (You can use the class defined above.) Example: (Project Name: ArithmeticUtility, Jar Name : ArithmeticUtility.jar, Package name: com.arithmetic, ClassName: Calculator)
Export it as a jar.
Create another java project add the jar by right clicking on the project-> Build Path-> Configure Build Path->Add External jars-> Apply-> Ok.
Use the class in your present project by mentioning the required imports. (Example: import com.arithmetic.*)
Please edit your code as below:
import add.*; // import statements should be at top after package outside
public class test{
public static void main (String[] args){
int x;
addTogether testObj=new addTogether();
int result=testObj.add(4,5);
System.out.println("Result: "+result);
}}
public class AddTogether{
public void addtogether(int a, int b){
return a+b; // your method has no return statement
}}
Hope it helps.

Java package and directories

It may be a similar to this question : package does not exist error!
but I don't understand how to manage it.
I try to follow this lesson (in French sorry) https://openclassrooms.com/courses/les-tests-unitaires-en-java
and so I have the following tree :
Garage/test/XXXTest.java, Garage/main/impl/XXX.java, Garage/main/inter/XXX.java
In test I have this code (GPSTest.Java)
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import main.impl.GPS;
public class GPSTest
{
#Test
public final void GPSTest() {
GPS gps = new GPS();
double prix = gps.getPrix();
assertTrue("Test prix GPS", prix == 113.5);
}
}
and in main/impl I have this one (GPS.java)
package main.impl;
import main.inter.Option;
public class GPS implements Option
{
public double getPrix()
{
return 113.5;
}
}
and in main/inter I have (Option.java)
package main.inter;
public interface Option
{
public double getPrix();
}
When I try to compile (I'm in Garage)
javac -cp "C:\Program Files (x86)\Java\junit-4.10.jar" test\GPSTest.java
I have this error
test\GPSTest.java:6: error: package main.impl does not exist
import main.impl.GPS;
Do I need to add Garage in the package name ? In the lesson (linked above) it's the same architecture and the same package name... But they use Eclipse, so maybe there are some differences (I use the command line)
EDIT
If I remove the test part it works :
test\TestGPS.java
package test;
/*import static org.junit.Assert.*;
import org.junit.Test;*/
import main.impl.GPS;
public class GPSTest
{
// #Test
public final void GPSTest() {
GPS gps = new GPS();
double prix = gps.getPrix();
//assertTrue("Test prix GPS", prix == 113.5);
System.out.println(prix);
}
}
With the following command doesn't give error... So I suppose the problem is with the classpath, but how can I fix it ?
javac test\GPSTest.java
Do I need to add Garage in the package name
No, but you need to be in the directory Garage when you compile, such that you are at the head of the following directory tree:
main
main/impl
main/impl/GPS.java
main/inter
main/inter/Option.java
test
test/GPSTest.java
The problem was with the clathpass. I had to add the current file to the path with .; before the rest of the path:
javac -cp .;"C:\Program Files (x86)\Java\junit-4.10.jar" test\GPSTest.java

how to solve package does not exist error in java

I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error
"package com.api does not exist".
Test.java
import com.api.APIUser;
public class Test{
APIUser ap = new APIUser();
ap .login();
public static void main(String[] args){
//to do
}
}
APIUser
package com.api
public class APIUser{
public string login(){
//to do
return string;
}
}
If any one have idea why I am getting this error.please suggest me solution.
Thanks in advance.
put a semicolon after the package com.api like as below
package com.api;
clean and build the project and run if any issue inform
You have multiple issues in your code.
You have no line termination present for the com.api import in the APIUser class;
You have a syntax error in your login method.
Below is the improved code:
import com.api.APIUser;
public class Test {
// APIUser ap = new APIUser(); // This call should be in the method body,
// there is no use to keep it at the class level
// ap.login(); // This call should be in method body
public static void main(String[] args) {
// TO DO
APIUser ap = new APIUser();
ap.login();
}
}
APIUser
package com.api; // added termination here
public class APIUser {
//access specifier should be public
public string login(){
//to do
//return string;//return some value from here, since string is not present this will lead to error
return "string";
}
}
Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.
You can use the code below to understand how to compile your class using classpath from command prompt.
javac -cp .;/lib/user.jar; -D com.api.Test.java

Categories