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
ADVERTISEMENT
Dec 24, 2014
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!!
View Replies
View Related
Jun 5, 2014
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.
View Replies
View Related
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
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
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
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
Mar 30, 2014
To be specific, how can I use enumeration of one source-code file in another.
View Replies
View Related
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
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
View Related
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
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
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
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
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
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
Jun 7, 2014
This is my class with the GUI:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
[code]....
Eclipse error message:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at GUI.<init>(GUI.java:26)
at Apples.main(Apples.java:7)
i think the problem is to do with my images not being recognised. I put them in my source in User>...>workspace>src which is correct as far as i know. From what i know the images should show up if i look at my src file in eclipse but they dont. I tried changing the file type from .png to .jpg but it makes no difference.
View Replies
View Related
Mar 17, 2015
I'm making a java program that can "convert" java to arduino by sending commands over a serial port. I'm using the JSSC library, and using methods to shorten things up a bit.
I've already made the code that starts up the serial port and it does connect. But I've made two different classes for the methods. One for the RXTX and one for the Arduino.
I'm making it so that I can just type:
Java Code:
public static void main(String[] args) {
RXTX.SerialSetup();
Arduino.pinMode(2, 0);
} mh_sh_highlight_all('java');
And it will start the serial port and set pinmode on pin 2 to output.
But when I try it I get this error:
Java Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
serialPort cannot be resolved mh_sh_highlight_all('java');
I don't know if its possible to 'send' a variable to a different method.
View Replies
View Related
Oct 23, 2014
Each time i create a new project or try to compile my workflow (run as GenerateDemo.mwe2 file by write clicking on the file the run as --> WME2 Workflow) it prompts the error I've attached the picture of.
What is it for? How can i get rid of it?
View Replies
View Related
Feb 8, 2015
I tried running this program but shows some compilation error of cannot find symbol . in following:
p1.num, p2.num and p3.num
cass Guessgames
{
void startgame()
{
Players P1=new Players();
Players P2=new Players();
Players P3=new Players();
boolean p1isright=false;
boolean p2isright=false;
boolean p3isright=false;
int guessp1=0;
[code].....
View Replies
View Related
May 5, 2014
I want to know if there is any general rule/pattern about things which give compilation fails and things which go for Runtime error/exception. OR only way is to remember all of them and there is no actual pattern in it.
View Replies
View Related
Nov 29, 2014
I'm having some difficulty getting this GUI dice game to compile. I'm using javac and notepad++ for this and I'm getting the following errors:
DiceGame.java:34: error: cannot find symbol
die2label = new JLabel();
^
symbol: variable die2label
location: class DiceGame
[Code] ....
4 errors
My code is as follows:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class DiceGame extends JFrame
[Code] ....
I'm sure the problem is something trivial, but I guess that's all part of the learning process that I have to go through.
View Replies
View Related
Mar 17, 2014
We are getting "Code too large" compilation error for one of our class. This class contains public String fields for label ID and value. We use this class for localization, except for English all other language labels come from .properties files.
The reason we are getting this error is because we have a static block in which using reflection we are populating a HashMap with all public fields and their value. The number of fields have gone up to the extinct where we are crossing the 64K limit for a static method. One of the most feasible solution was to use .properties files for English labels as well.
I will be calling this class MyLabels. We defined a super class for MyLabels called MyLabelsExt. And now we are adding labels into the super class instead of the MyLabels. By running some tests we confirmed that the map that we initialize in MyLables class contains all the fields from both MyLabels and MyLabelsExt class.
How is the 64K limit error not coming if the labels are defined in a super class. Does that mean Java is able to identify that some of the fields are coming from parent class, and that is being treated as separate from the child class. And how is the map that we initialize having all the value.
View Replies
View Related
Apr 20, 2014
I have a simple doubt
float k = 0;
k+=0.2;
k=k+0.2; // here compilation error
compliation error Type mismatch: cannot convert from double to float
My question is why not a complilation error at k+=0.2;
View Replies
View Related
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.
View Replies
View Related
Jun 20, 2014
I notice when I look to download library jars, I keep ending up at Maven Repository: Search/Browse/Explore. I wonder is this really the de-facto place to download jars for java libraries? Right now I want to use the apache commons net library in order to build an imap server. And I find myself again mvnrepository:Is this where I should be downloading jar files?
View Replies
View Related