A Public Class In Sub-folder Is Not Getting Found By Another Class While Compilation
Feb 14, 2014
I was doing coding exercise from a book ('OCP Java SE 6 - Practice Exams' by Kathy Sierra and Bert Bates). I came to a question that told to demonstrate the difference between 'default' and 'protected' access rules by creating/making a directory structure and putting a couple of classes in different packages.
For this, I made a total of four classes, out of which, three classes are-Car, TestingCars, CarDimensions. (The fourth is not yet used in testing code till now, so, I am giving only the other three classes.) Their coding is given below.
Out of these classes, the classes- TestingCars and Car - are in a directory (say, FolderName). And, the class- CarDimensions is in FolderName's sub-folder.
The class 'CarDimensions' is public (and its components too are public). And, I am testing all the classes from the class- 'TestingCars'. But, this class (TestingCars) is not able to find the public class- 'CarDimensions' which is in its sub-folder and gives two 'Cannot find symbol' errors citing the class-CarDimensions. Also, If all three classes are put in one single directory, the programs work, without any error.
Coding:
Class TestingCars:class TestingCars {
public static void main(String[] args) {
Car c = new Car();
c.setType("FourWheeler");
[Code]....
I could not find why the public class- CarDimensions- is not getting found by the TestingCars class.
I've a .java file that won't compile, but produces no errors (in cmd prompt).
I think its the import of java.util.ArrayList thats causing the problem (because it can compile a different file in the same source folder) - so i'm assuming its the classpath that is wrong. which is fine. i love fighting with classpaths.
But why isn't it providing me with an error. the compiler usually goes bat-sh.. crazy if the -cp is incorrect!
Its because I'm switching between command prompt and a text editor and it hadn't saved the file for some reason, and still won't am getting rid of this editor!!
Am facing a very strange issue. While trying to compile a very simple Hello World java program, the compilation completes successfully without any error or warning, but it does not generate the class file.
It happens when I compile with a particular jar file, otherwise compiling only the program (or with any other jar) does generate the class file. I am using java 1.7.0_45.
The compiler won't let me declare more than one class as "public". Am i correct in understanding that this is a java restriction ? This means i need to create a new file, for each public class that i want in a package ? The rest of the classes without access modifier will all be package-private. (Q has been asked before probably, but my search could not be narrowed).
Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:
Java Code:
public static Exam[] collateExams(Exam[] exams){ Exam [] r = new Exam[50]; r = exams; Exam [] finalExam = new Exam[50]; for(int i = 0; i < r.length; i++) { if(getExamType() == 'M') { } } return r; mh_sh_highlight_all('java');
I have a question about the following snippet concerning the steps the javac compiler follows to compile a program:
[...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]
It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)
The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes.
However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class. I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?
However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:
NonpublicClass.java:
Java Code:
package com.work.company; class NonpublicClass { public void description() { System.out.println("Working!"); } } mh_sh_highlight_all('java');
[Code] ....
Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:
Java Code: NonpublicClass v = new NonpublicClass(); mh_sh_highlight_all('java');
I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:
Within my program I allow users to write their own code to be executed. I then save that code in a .java file and then compile. This gives me a .java file and a .class file within the same package in the src folder. However, the compilation process also creates a .class file in the build folder. When the user edits their code and then re-runs it, the .class file in the build folder does not get updated with the new data, but the .class in the src folder does.
What I want to do is to execute the .class file in the src folder and not the build folder. So far I have the following method, but that somehow refers to the .class file in the build folder:
public void runMethod(String packageFilePath, String className){ try{ URI fileURI = new URI("file:/src/uk/learningAid/UserInputs/"); URL url = fileURI.toURL(); URL[] urls = new URL[]{url};
If i have a class(lets say class name is Approval) with the following private members: String recip_id, Int accStat, String pDesc, String startDate How can i create public get and setter methods for these private members of the class?
I have a jar file.and i imported its classes to my program..
let say i have a code
source code is at /home/t_bmf/Java/src import firstjar.FirstJarPrint; public class TestJar { public static void main(String[] args) { //FirstJarPrint jar = new FirstJarPrint(); } }
well i have successfully compiled it using command below:
it means that i don't have any compilation error right?so all classes found properly.But whenever I run the program I always got this error:
java -cp /home/t_bmf/Java/lib/FirstJar/jar:. TestJar Exception in thread "main" java.lang.NoClassDefFoundError: firstjar/FirstJarPrint at TestJar.main(TestJar.java:6) Caused by: java.lang.ClassNotFoundException: firstjar.FirstJarPrint at java.net.URLClassLoader$1.run(URLClassLoader.java: 202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06) at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47) ... 1 more
I think it has to be no error since i have compiled the program successfully right?if it really didn't find the class, then it must be a compilation error right?
is there something wrong with the way I execute the program?
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Session at SendEmail3.main(SendEmail3.java:15) Caused by: java.lang.ClassNotFoundException: javax.mail.Session at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
I am validating an xml file using XQuery in Java using XQJ API. When the query is triggered it is giving the ClassNotFoundException for orai18n.text.OraCollator. I have placed the orai18n-collation.jar file in MANIFEST.MF file and it is loaded in to the class path.
Not sure on why it is giving the exception.
The JDK i am using JDK1.7 and application server is weblogic.
So I'm learning java having been using c#. I based this code off an example from class. It compiles fine with no errors, but I'm getting this:
Which model do you want? + Standard,Electrum,CurveHilted, or Tonfa Standard Exception in thread "main" java.lang.ClassNotFoundException: Standard at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method)
I have written a java applet. Few months before It was working all fine but my client has some other requirements now and I have to edit it. I am getting two problems:
1. I could not execute it on my local computer as it always gives "your security settings have blocked a local application from running". I have edited the settings from Control Panel but it is then started giving permission error on including permission in manifest file it started giving trusted library error and still it is not resolved.
2. Can I know how to work with third party library with applets. I have imported the library and uses its few classes but when I tried to load applet it always give no class definition found error. I have some ways mentioned online like use comma separated names for all the jars but no luck so far.
How do you declare methods for a class within the class whilst objects of the class are declared else where?
Say for instance, I have a main class Wall, and another class called Clock, and because they are both GUI based, I want to put a Clock on the Wall, so I have declared an instance object of Clock in the Wall class (Wall extends JFrame, and Clock extends JPanel).
I now want to have methods such as setClock, resetClock in the Clock class, but im having trouble in being able to refer to the Clock object thats been declared in the Wall class.
Is this possible? Or am I trying to do something thats not possible? Or maybe I've missed something really obvious?
Regarding the lifecycle of servlet , in headfirst servlet i can find :
You normally will NOT override the service() method, so the one from HttpServlet will run. The service() method figures out which HTTP method (GET, POST, etc.) is in the request, and invokes the matching doGet() or doPost() method. The doGet() and doPost() inside HttpServlet don’t do anything, so you have to override one or both. This thread dies (or is put back in a Container-managed pool) when service() completes.
How can I call the doGet method of the subclass from the superclass. i am not getting this .
I have a quick polymorphism question. I have a parent class and a sub class that extends the parent class. I then declare an array of parent class but instantiate an index to the sub class using polymorphism. Do I have to have all the same methods in the child class that I do in the parent class? Here is an example of what I mean.
public class ParentClass { public ParentClass(....){ } public String doSomething(){ } } public class ChildClass extends ParentClass { public ChildClass(....)
[Code] ....
Is polymorphism similar to interfaces where the child class needs all the same methods?
Where can I learn or how can I, being the most efficient way known to do so, create a folder outside my JAR file with the java source code, this then will copy YAML documents from my JAR file to that folder, then I need to read the YAML documents some way. I'm making an addon for a game, I am using an API that allows you to make a config.yml easily, and add and read entries from it, but I've read that I need to make my own methods to be able to create additional YAML documents.
I don't exactly know what to type in google to perhaps find such a page, but I did try to find a tutorial page about this or something and couldn't.
I've partially figured out how to create a folder, but I have a problem, how can I RETURN one directory to make the folder, I don't want to make the folder in the JAR file I want to make it just outside the Jar file in the same folder that the Jar file is at.
I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator
class A{ public void hello(){ System.out.println("hello"); } } class B extends A{ public void hello(){ //super.hello(); System.out.println("hello1");
i want to write a class in such a way that i should get the current execution time of another class which is running. I searched in net but it shows only how to calculate the time duration of the current class which is running. But as per my way, i need the execution time of one class from another class. How to do this ?