Cannot Be Accessed From Outside Package Error Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack how to access a class in different package Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack java class is not public in package Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Classes cannot be java cannot access class file accessed from outside package up vote 6 down vote favorite I have two packages. The class I want to import from the first package is declared as PUBLIC. Despite, when I test a file from the second package it shows me compilation
Java Compile Error Cannot Access Class
errors like this: PUBLICclass is not public in mypackage; cannot be accessed from outside package I tried to add a public constructor to the class from the first package, but it doesn't make any difference. Do you have any ideas? I use Netbeans 7. The class from the first package looks like below: public class PUBLICclass extends AbstractClass { public PUBLICclass() { } } java share|improve this question asked Dec 5 '11 at 14:12 deem 58621129 3 Can you add the class declaration (class XYZ java error cannot access together with the constructors) to the question? –Jeff Foster Dec 5 '11 at 14:13 Also, make sure there's a space between the keyword "public" (no need to capitalize it) and the class name. –andri Dec 5 '11 at 14:14 2 Did you, by any chance, forget to import this PUBLICclass in the Java file which uses it? (Outside of the package where PUBLICclass is located.) –user268396 Dec 5 '11 at 14:23 @user268396 I have it, and the error is about that line just. –deem Dec 5 '11 at 14:24 What I meant is that it looks like you imported the class using something like “import PUBLICclass;”, when you need something more like import mypackage.name.PUBLICclass;. –user268396 Dec 5 '11 at 14:26 | show 3 more comments 3 Answers 3 active oldest votes up vote 17 down vote accepted Let me guess Your initial declaration of class PUBLICClass was not public, then you made it `Public', can you try to clean and rebuild your project ? share|improve this answer answered Dec 5 '11 at 14:28 mprabhat 14.2k22451 LOL I've just cleaned and rebuilt the project and there is no problem now. –deem Dec 5 '11 at 14:50 It just happens sometimes ;) –mprabhat Dec 5 '11 at 14:51 1 In my case, restarting the IDE (I'm using Android Studio) helped. –Lee Han Kyeol Mar 3 '15 at 12:53 add a comment| up vote 0 down vote Do you by any chance have two PUBLICclass classes in
New Topic programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering constructor cannot be accessed in class Languages Frameworks Products This Site Careers Other all forums
Java Error Cannot Access Class
Forum: Beginning Java error: cannot be accessed from outside package Rob Deer Greenhorn Posts:
Bad Source File Error In Java
17 posted 9 years ago I have this code that's giving me error. /home/dev/WEB-INF/classes/db/mysqlconnector.java package db; import java.sql.*; import java.lang.*; import java.io.*; import com.javaexchange.dbConnectionBroker.*; http://stackoverflow.com/questions/8386576/classes-cannot-be-accessed-from-outside-package public class mysqlconnector{ public DbConnectionBroker broker = null; public Connection database = null; public Boolean isConnected = false; mysqlconnector() { try{ broker = new DbConnectionBroker("com.mysql.jdbc.Driver","jdbc:mysql://127.0.0.1:3306/mydb","mydb","password",2,20,"/home/dev/logs/dev.connectionbroker.log",0.01); database = broker.getConnection(); } catch(IOException ioe){} } public Connection Connect(){ if(this.database != null){ this.isConnected = true; } return this.database; } //protected void finalize(){ public https://coderanch.com/t/406122/java/java/error-accessed-package void Disconnect(){ if(this.isConnected == true){ try{ this.broker.freeConnection(database); } catch(Exception e){} } } } when i compile this below, /home/dev/WEB-INF/test.java it gives me the error mysqlconnector() is not public in db.mysqlconnector; cannot be accessed from outside package mysqlconnector c = new mysqlconnector(); test.java import db.mysqlconnector; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.lang.*; public class test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { mysqlconnector c = new mysqlconnector(); c.Connect(); } } Any ideas on what's wrong? [ February 10, 2007: Message edited by: Ester Wong ] Anton Uwe Ranch Hand Posts: 122 posted 9 years ago Replace mysqlconnector() { try{bypublic mysqlconnector() { try{in your mysqlconnector.java. Rob Deer Greenhorn Posts: 17 posted 9 years ago Ah, thanks! I'm blind as a bat. Post Reply Bookmark Topic Watch Topic New Topic Similar Threads Servlets classes and borkers classes
to Access Class from outside package Aug 12, 2012 2 Comments J Singh read Here we will learn how to http://javabeginnerstutorial.com/learn-by-example-3/how-to-access-class-from-outside-package/ access Class with different AM(Public / Default) from different package 123456789101112 package com.accessmodifier;/* * Here we will learn how to access Class with different AM(Public / Default) from different package */public class AccessClassDifferentPackage {}class AnotherClass{} Class in different package 1234567891011121314151617181920212223242526272829303132 package com.accessmodifier2;import com.accessmodifier.AccessClassDifferentPackage;/* * If you try to import default/non public class compiler will give cannot access error * "The type com.accessmodifier.AnotherClass is not visible" */import com.accessmodifier.AnotherClass;/* * Here we will try to access Classes with different access modifiers. * Note*: This class is in different package. See Above */public class AccessClassFromHere { public static void main(String args[]) { //Public class can be accessed without any problem AccessClassDifferentPackage class1 = new AccessClassDifferentPackage(); /* cannot access class * Default class can not be accessed from here as it is not visible. * Compiler will give error "AnotherClass cannot be resolved to a type" */ AnotherClass class2= new AnotherClass(); /* * Even importing the class will not make any difference. See Above */ }} WRITTEN BY J Singh Main brain behind this blog/QnA site. She has written all these article as per her personal experience. She has published it mainly to help beginners. These articles might have some problems. So bear with her and let her know if you find any problem in any of the article. NEXT READING Learn By Example How to access from same package Aug 12, 2012 1 Comment J Singh read PREVIOUS POST← How to access from same package NEXT POSTAccess Variable from other class in same package → 2 Comments → How to Access Class from outside package Martin Dec 8, 2015 at 1:11 am Hi, Can we access non-public class using import? It is not