Can Access One Class Present In One Source File

Nov 3, 2014

I am having hard time to grasp the concept of java as i am beginner. according to different sources found in internet, only one class is written in one source file. and all those class can be accessed through the main class.
my question is

1.can we access one class present in one source file, through another class present in another source file [not through the class containing main method]?

2.can we create more than 1 class in same source file?is there special way to do it? i do get error always when i try to do so
3.can multiple classes contain main method? or should there be only single class containing it?

View Replies


ADVERTISEMENT

Java Source Code File Naming With Access Modifiers

Feb 3, 2014

Why is this not valid in java:

Both uses public with the same class/interface name.

Test.java:
public class Test implements Test{
/// Some codes
}
public interface Test {
///Some methods
}

View Replies View Related

Defining A Class In Source Code File?

Oct 9, 2014

"If you define a public class or an interface in your class, then it should match the name of the java source code file."

Does the statement above mean the following should compile fine ?

public class Animal{
public class Animal{ }
}

The author gives an example saying that if you have a class Multiple.java and in that class, you have:

public class Multiple{}, then it should compile.

But this does not compile. I tried it.

View Replies View Related

Non-Public Class In A Source File Doesn't Matching Its Name

Oct 20, 2014

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:

Java Code: javac NonpublicClass_different_name.java mh_sh_highlight_all('java');

Then a proper class file is generated with the class name, that is NonpublicClass.class, and afterwards I can compile the main program in Start.java.

So the question is what am I missing regarding the cited quote that reads:

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.

? Because adding such an import to CompanyClass.java:

import com.work.company.NonpublicClass;

didn't work either...

View Replies View Related

Jar Should Be Present To Execute Main Class

Mar 18, 2014

I have few queries regarding compiling v/s execution of a java program. Suppose I have a Main Class which is referring to class Test which is present in JarA. Now, at compile time, this jar JarA must be present in classpath/buildpath. So, what actually is the action performed by the compiler by referring to this jar?

Now, at runtime also, this jar should be present to execute the Main class. Is this correct? If yes, then what is the action performed by JRE when it refers to this class at runtime? WOn't all the dependencies be resolved at compile time only?

View Replies View Related

Swing/AWT/SWT :: Invalid Thread Access - Unknown Source

Apr 23, 2014

I am new to threads, This is a project about 2048 game to be more accurate, and i want this project to run in this thread that i make in main...

My main:

public class Execute {
public static void main(String[] args){
Display d = new Display();
Shell s = new Shell(d);
Model m = new Game2048Model();
View ui = new Game2048View(m.getBoardArr(),d,s);

[Code] ....

And the error is:

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Display.error(Unknown Source)
at org.eclipse.swt.widgets.Display.checkDevice(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at view.Game2048View.run(Game2048View.java:55) //problem at Run
at java.lang.Thread.run(Unknown Source)

Here is the problem....

@Override
public void run() {
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}

View Replies View Related

Access Denied To Create Class File

Aug 10, 2014

I am new to java. While compiling the "moooooo" program an error is displayed.

Error -- error while writing MooseGreetings :MooseGreetings.class
<access denied>.

What to do?

View Replies View Related

Cannot Access Class File From Package Inside JAR

Mar 7, 2015

I'm trying to access class file which is inside the package and package is inside the jar file but I can't access that class file inside my source file . All files are on desktop .

View Replies View Related

Can A Class Be Broken Into Multiple Source Files?

Apr 3, 2014

The problem is what to do when the source file for a class gets too big ( a judgment call for sure). Is it possible for one class to be defined in multiple files and if so how do you do it, and is it good practice?

I do see ways to refactor into a base class or move code into a helper class but sometimes the abstraction is cleaner as a single class that does a lot of stuff. My problem is how to organize a BIG class?

View Replies View Related

Extract File In Same Directory As Source File

Jun 13, 2014

This is a snippet code that im developing. (no source of it) . I am facing a trouble to extract file in the same directory as the source file (example: extract here option)

public class HelperAction {
public static void unzip(String zipFilePath)throws IOException {
File dir = new File(zipFilePath.substring(0,zipFilePath.length()));
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
FileInputStream fis;

[Code] ....

View Replies View Related

Copying From One File (source) To Another File (destination)

Jul 5, 2014

I have a file(source.txt) that contains some texts. Now, the java code is to run through each line in the source file and check if that line begins with a number and if true, copy that line to the destination file(destination.txt). I have the code but I am not sure why I can't loop through each line in the source.txt file. Below is my java code:
 
public static void copyFile(File source, File destination) {
BufferedReader br;
try {
FileWriter fw = new FileWriter(destination.getAbsoluteFile());
PrintWriter bw = new PrintWriter(fw);
br = new BufferedReader(new FileReader(source));
String line = new String();

[Code] ....

As shown in the source.txt file, there are 4 lines in the file but when the execution gets to while ((line = br.readLine()) != null) , it executes to false.. Why this is so?

below is the source.txt file

12 is a number
Green is bad
5 is not so cool
you are right

View Replies View Related

Access Getter / Setter Of Bean Class Where It Defined As Member In Another Class?

Feb 18, 2014

Class UserAssessBean{
private String username;
private int userid;
private ArrayList<ModuleBean> module;
--{get/set}--

[Code] ....

How can i access the getters/setters of module bean, when it was returned as array list in UserAssessBean?

View Replies View Related

Add Source To Bat File?

Aug 30, 2014

Have the following .bat file that compiles the ReadMindWave.java source with jna and jfreechart. I get a javac error saying 'no source' when the .bat is run, as the .java source needs to be included in the .bat file. Where it goes in the following .bat file?

@echo off
echo Compiling %1 with JNA and JFreeChart...
javac -Xlint:deprecation -cp "C:jna-master*;C:jfreechart-1.0.19libjfreechart-1.0.19.jar;C:jfreechart-1.0.19libjcommon-1.0.23.jar;."%*

The file to compile is in C:MindWaveTestsReadMindWave.java

View Replies View Related

Finding Source Files In Other Directory With Class Path Option

Apr 23, 2015

I'm having another issue. I have 2 java source files(see below). They are place in the same directory. How do I compile them using classpath?

I have already tried 1st attempt :

javac -cp com.companyname.interview.DuplicateReplace.java DuplicateReplaceTest.java [did not work!]
2nd attempt: javac -cp DuplicateReplace.java DuplicateReplaceTest.java [again, did not work!]
package com.companyname.interview;
public class DuplicateReplace { /* code */}

[Code] ....

View Replies View Related

Source File Input

Jun 8, 2014

how to add an error message when my program can't find the file the user inputs.

For example:

Enter your source code:
D:Data StructsProjectssrcHelloWorld (I didn't put .java on purpose)
Exception in thread "main" java.io.FileNotFoundException: D:Data StructsProjectsAssignment 3srcHelloWorld (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at HomeworkDriverFour.main(HomeworkDriverFour.java:14)
//Can I get rid of the exception above and replace it with "File not found!"

Enter your source code:This is what I have so far:

import java.io.*;
import java.util.*;
public class HomeworkDriverFour {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

[code]...

View Replies View Related

Source File Compilation

Jan 22, 2015

when i try to compile following source file, there is a error which states "MovieTestDrive is public, so must be declared in a file MovieTestDrive.java" i didnot understand. i am beginner in java;

class Movie
{
String title;
String genre;
int rating;
void playit()
{
System.out.println("playing the movie");
}
}

public class MovieTestDrive

[code]....

View Replies View Related

Using Enumeration Of One Source-code File In Another?

Mar 30, 2014

To be specific, how can I use enumeration of one source-code file in another.

View Replies View Related

Source File - Arithmetic Operator Substitution

Nov 6, 2014

My goals:

1) Have some source file be read in

2) Specify what arithmetic operators to swap (+, -, /, *)

2) If an arithmetic operator is read (like a + sign etc) then we swap it with its opposite (- for example)

3) Once the swap is complete, the rest of the file stays the same even if more operators are in the file...it is then output to a file (I am going with 1mutation.java)

4) This is where it gets tricky....it then picks up where it left off to finish reading the + operators (or whatever was specified) and repeats steps 2-3 (but the operator that is already swapped gets left as it was / skipped) and the output is saved as 2mutation.java.

The most I have been able to manage is having it changed 1 operator or all of them at once. I deleted a lot of my work to start fresh / master one operator for the time being. Here is what I have:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
 
public class OperatorSub {
 
[Code] ....

This is the data file I am using. (see attached) The file should be .java or .cpp but I stuck with .txt for now. How to tackle this? Am I on the right track?

View Replies View Related

Swing/AWT/SWT :: Loading Image File From Source Directory?

Jan 25, 2014

My problem is that I want to load an Image from the source directory in Canavas' paint() method.

My code is currently (in a try/catch)

g.drawImage(ImageIO.read(new File(imageLocation)), imageX, imageY, null);

Where imageLocation leads to an image on my HDD ("C:UsersVladdeDocumentsFolders-Filesfolder.png")

View Replies View Related

How Java Finds Jar Contents Imported In A Source File

Feb 10, 2015

how java imports libraries from a jar file inside a source. Let's say I have the following code on Something.java file,

import com.mycomanyA.*;
import com.mycomanyB.*;
public class Something{
public static void main(String ... args){
// code goes here...
}
}

And the Something.java is inside the following structure...

A.jar
com/
mycompanyA/
A.class
Another.class
B.jar
com/
mycompanyB/
B.class
neededImage.gif
bin-folder/
org/
apache/
Something.java

To compile this code(Something.java) from command line I want to use the following command,

javac org/apache/Something.class

And for the compilation to be successful, I'll have to be just above bin-folder because...

- > Only then my default class path will be (.) and java would automatically include A.jar and B.jar(the other way is to add the jar files using -cp argument, which I want to avoid in this scenario)

- > And for the import org.mycompanyX to work I'll have to be in the (.) directory.

Are my assumptions correct about how java find jar contents imported in a source file?

View Replies View Related

Read In Java Source Code File As Command Line Argument

Oct 31, 2014

I am trying to complete this question. I understand the most of it but I haven't go a clue to read in the file name.

Full question: Implement a program that reads in a Java source code file and checks to see if it has balanced {}brackets. Your program should use a stack, implemented as a linked list, to check the brackets.

NOTE: you can use a reference called top which points to the head of the list. Your program should run as a command line program and should take a filename as an argument and print one of BALANCED or NOT BALANCED.

For example: c:> java checkBalanced "myProgram.java" BALANCED

View Replies View Related

Read Text File Into Object Array And Creating Random Access File

Dec 8, 2014

I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...

The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.

Here is the base Product class that must be used to create the objects for the array.

public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;

[Code] .....

And then here is the data from the text file that i must extract to use to create product objects.

Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19

These continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quantity after the second comma.....

View Replies View Related

Reading Text File Into Object Array And Create Random Access File

Dec 9, 2014

I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...

The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data. Here is the base Product class that must be used to create the objects for the array.

public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;

[Code]...

these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....

View Replies View Related

Reading Text File Into Object Array And Creating Random Access File?

Dec 8, 2014

I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...

The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.

Here is the base Product class that must be used to create the objects for the array.

public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;
//Constructor
public Product( String pName, double price, int quanity )

[code]....

and then here is the data from the text file that i must extract to use to create product objects.

Dill Seed,938,34

Mustard Seed,100,64

Coriander Powder,924,18

Turmeric,836,80

Cinnamon (Ground Korintje),951,10

Cinnamon (Ground) Xtra Hi Oil (2x),614,31

Cinnamon (Ground) High Oil (1X),682,19

these continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quanity after the second comma.....

View Replies View Related

Creating Inventory File As Random Access File

Oct 26, 2014

I need to write a program that will create an inventory file as a random access file. The problem that I am having is that among the things I need to add to the random access file is a "car name". Since random access files are byte based I don't know how I could possibly write and then read a string value.

View Replies View Related

JSP :: How To Check If URL Parameter Is Present Or Not In Request

Apr 3, 2015

I want to run a function on load only when a particular parameter is not present?

My question is how to detect in JSP that if a particular parameter is present or not?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved