I'm trying to compile a java file which imports other packages I created; however, it doesn't seem to find them.
In my compile.bat file I have:
set classpath=c:\t\DB;c:\t\Frame
javac comchange.java
where the beginning section of commChange.java has
package commchange;
import java.sql.*;
import java.awt.event.*;
import java.applet.*;
import DB.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics;
import Frame.*;
and the directory structure is:
c:\t\commChange.java
c:\t\DB
c:\t\Frame
The error I'm getting is:
commChange.java:12: package DB does not exist
import DB.*;
commChange.java:17: package Frame does not exist
import Frame.*;
commChange.java:23: cannot find symbol
symbol: class Frame
...
Any ideas?
classpath is the list of directory roots where classes, identified by package.ClassName, are loaded from. You need to set the following classpath:
set classpath=c:\t
I have a couple of remarks (as many things are actually wrong):
Traditionally, packages have all lower case names i.e. db, invoicechange, frame, etc.
Sun coding standards require classes to begin with a capital letter i.e. commChange should be named CommChange and the compilation unit should use the same name CommChange.java.
Source files should be arranged in a directory tree that reflects their package tree which means that invoicechange.CommChange should be located in C:\t\invoicechange\CommChange.java.
Once you'll have done these changes, you'll be able to compile your classes. To do so, either define the user class path explicitly in the CLASSPATH environment variable to include the root of the sources tree:
C:> set CLASSPATH=C:\t;%CLASSPATH%
And just call javac from the C:\t directory:
C:> dir
invoicechange/ db/ frame/
C:> dir invoicechange
CommChange.java
C:> javac invoicechange\CommChange.java
C:> dir invoicechange
CommChange.class CommChange.java
Note that if you don't set the user class path (and thus don't override the default class path), javac will use the current directory as default. In other words, calling javac from C:\t without setting the user class path in CLASSPATH environment variable will just work.
See Setting the class path for more details. Actually, you should also look at the documentation of javac. And reading the Sun coding standards previously mentioned would be a good idea too.
You have at least three big problems. First, the classpath needs to point to the "root" folder as mentioned in the first answer. When you import DB, then it needs to start looking in the folder called t. (It bothers me a little, though, that the error message you posted, lists Import DB.*; in the error message, with Import highlighted like a class name instead of a keyword.)
Second, there is no Frame package, so the import statement that tries to import Frame.* doesn't make any sense at all. If you want to import the Frame class you can import java.awt.Frame;, but you already have a wildcard import for the java.awt package so you don't need that.
Finally, the file comChange.java must be located in the folder C:\t\InvoiceChange, not in the C:\t folder. That's because it belongs to the InvoiceChange package.
Related
I am pretty new to Java and Linux. I can't use an IDE but i have jdk installed (obviously). I have three .java files that i want to compile. One is the main code file and two small classes. how do i compile them using terminal?
these files are called:
main.java
object.java (Object.class when compiled)
living.java (Living.class when compiled)
object.java and living.java only have a constructor for now that i want to call
i've tried
javac main.java #this seems to be the right one
javac main.java object.java living.java
javac main.java Object.class Living.class
in terminal and
import object.java;
import living.java;
import Object.class;
import Living.class;
import object;
import living;
import Object;
import Living;
in the main.java file
but nothing seems to work
when i use
import Living;
in the code it tells me that it misses a ; or .
, when using precompiled
import Living.class
in the code i get
error: class, interface, or enum expected
import <Object.class>;
in the terminal and when i try
import living.java
in the code i get
error: package living does not exist
import living.java;
in terminal
so what am i doing wrong? do i have to import precompiled classes or java codefiles? do i have to tell javac all files i want to use or only the main.java file? main.java compiles without error when i don't try to import one of the classes. And if i have to use .jar files please explain and give an example
Your file name has to match the class name, e.g. if you have a class Living {... your file name has to be named Living.java. Be aware of the same character casing here. If you use package xyz; in Living.java, you also have to place your file in the subdirectory xyz (e.g. xyz/Living.java).
Importing is to be done by import Living;, with the same case. On using package xyz; in your Living.java, you have to use import xyz.Living;.
Classes within the same package doesn't need to be imported.
You compile your files by using javac Living.java or with package javac xyz/Living.java. The javac will produce the Living.class/xyz/Living.class file.
Same with Main.java.
To run a classes main method, you have to run the java executable with the class name, which contains the static void main(...) method, e.g. java Main (or java xyz.Main if Main has a package xyz;).
Never create an Object.java, since Object is already reserved...
BTW: maybe you follow one of the many tutorials available online, to get a first glance on java...
as #Arnaud commented: "Note that if all three classes are in the same package, you don't need to import them in your code"
i don't need to import these classes in this case and leaving import away works.
I have the In package from Princeton loaded into the same directory as my files, and I compiled it.
And, I use the package in my code. But, when I use import In; somehow I still get an error?
java:7: error: '.' expected
import In;
^
What is the solution to this silly problem?
The code you linked to has no package.
Just delete import In, and somewhere in your code create an instance In myIn = new In(myUrl);, and you should be good.
Alternatively, modify your copy of "In.java" and make it the same package as you're using for the rest of your code.
Look at the main() in the code for examples of how to use class "In".
In order to fix this problem, you either need to add a package declaration to In.java that is the same as your package (and then simply omit your import statement), or (my recommendation) you need to add a package declaration to In.java that is different than your package (and move it to the corresponding folder), and then import In by the name of the package that you've given it.
To make this more concrete:
Add the following to the top of In.java:
package edu.princeton.cs.introcs.in;
Then move it to the corresponding "edu/princeton/cs/introcs/in" directory.
(Create it if it doesn't exist).
Then, in your file, import it by its qualified name:
import edu.princeton.cs.introcs.in.In;
Note that, in both of the cases above, you'll need to compile In.java along with your code that uses it (or you need to compile In.java, first, and ensure that it is on the classpath when compiling your code that uses it), and at runtime, you need to bundle In.class with your code (e.g. in the JAR you produce) or similarly guarantee that the byte code for that class (either the .class file or a JAR containing the .class file) are on the class path when executing your compiled code.
So I made a folder called util and placed four classes along with program named unit10Assignment in it. I created a package util and typed " package util; " at the top of each one of the classes code like this:
package util;
public class Employee
Then i wrote:
import util.*;
import javax.swing.JOptionPane;
public class unit10Assignment
On top of the program. However when I compile it, it tells me. Anyone know why? I tried playing around with it and it disappeared when I typed in import java.util*; instead but I'm not sure that what my teacher wanted as her example did not have the java in front.
It also says " bad source file" "package does not contain class Employee " However, everything compiled and ran perfectly before I typed in the package statement and I have not made any change to the code since then. If I removed the package statement from the employee class tho, the same message would appear but it would say another class does not exist.
Thanks for any help
Note: whether or not i put java.util or just util, this problem with the bad source still appears.
thanks for any help
I'm going to make the assumption that you have your project set up like this:
util/
Employee.java
unit10Assignment.java
bin/
(If it isn't, that's fine - so long as they're in some folder. bin/ should exist, though.)
The way that packages work is that they're folders on the hard drive - the package you want to import requires that the folder and class you wish to import both exist in that specific folder. This is why packages are handy - you can have two classes named Employee and have them live in completely different locations.*
Here's how you compile these into a package-like structure without the use of an IDE. Substitute $HOME for the full path of your Java class folder.
javac -sourcepath $HOME/util -d $HOME/bin *.java
And here's how you run your main class:
java -cp $HOME/bin util.$MAIN_CLASS
A breakdown of what these flags mean:
-sourcepath instructs javac to look in this specific directory for your source files.
-d specifies an output directory for your .class files.
-cp instructs java to add this folder to its classpath.
*: Really, really large projects can often use the same name as other classes; if you wanted to use a specific one, you'd have to use the fully-qualified class name for it.
Make sure that:
the filename matches the class name (e.g. Employee.java for class Employee)
the files are inside the corresponding folder according to their package (i.e. util)
Are you using any IDE? If not, using one realy helps a lot with this kind of things.
I'm not sure how to import a file from a directory above. That is, I have a setup like so
directory: MyProject
Main.java
directory: Other
Other.java
Basically, Main.java is in "MyProject" and Other.java is in a folder inside the project's root folder. I can easily do
import Other.*;
to get those files available in Main, but how do I get Main.java to be visible to Other.java?
import ../Main.java
Obviously this doesn't work, but that's the general functionality I'm looking for. Any suggestions? I would prefer not having to use absolute paths. Thanks!
Edit: I meant import not include. Sorry. Been using C++ too much.
Java does not include files. You can however directly use classes using the simple name by using import statements.
Basically you need a file per (top level) class you define. This allows IDE's to rename compilation units, and do other refactorings. Besides that, it lets you easily add code at the right spot.
Java does use packages to create namespaces. Packages themselves are completely separate namespaces. Although the namespace seems to be a tree structure, in Java each package is actually not related to any other package. Hence you cannot use it as a folder structure, using .. is not allowed. This may change once "super packages" are introduced.
The Java import statement looks a lot like #include, but the name change is deliberate: instead of grabbing the file to make the definitions in that file known, it is simply a statement to make it easier to refer to classes and interfaces. It has no other effect than having a shorter name to a class (or, for import static, constants and other static members).
Most of the time the top level classes are represented using a folder structure that reflects the package name. This makes it easy for IDE's and developers to find the file representing the class. It also makes it easy to put in version control. It is however not part of the Java specification itself; the location of Java source and classes is not defined. Earlier IBM IDE's actually stored Java source and classes in a database for instance; they did not use files at all. Newer IDE's such as Eclipse may use different source folders, e.g. one for Unit test files and one for the library itself.
So finally, the only way to include packages is by specifying the full package name, then a dot and then the class to import, or the * wildcard to import all classes of that package.
import java.util.Vector;
import java.util.*;
Most IDE's will create these import statements for you, possibly after you have chosen the right class to import (in case there are classes with the same name in different packages).
More information can be found in the Java Language Specification (Java 7 version).
In your case you have defined a Main class in the root or default package which is strongly discouraged. You can directly refer to Main without any import statement. The Other class is in the identically named Other package (using uppercase in package names is strongly discouraged as well). You can refer to it by using import Other.Other.
include ???
Java doesn't have file source inclusion support, it rather use a naming conversions, so you should import the namespace (package) that you need in your source file.
You should define a package for your main class and then import it in the Other class .
the Main.java is in the default package, this is impossible to import from other (named) packages
put it in a package and import as normal
directory: MyProject
directory: base
Main.java
directory: other
Other.java
(also package names are lowercase normally)
if you have file outside of your project it means this file:
wouldn't be compiled by project
wouldn't get into jar
can't be used in runtime
so you really shouldn't include it.
Either move it into project, or include dependent project which contains that file.
Java is not like C++. You include by package name. So if toplevel file is in project AAA in folder src/aaa then you should include that project as dependent jar and refer to file as import aaa.Main
I think import Main; should just work.
You should read up java concepts package and classpath. Please look at the documentation here. The options that will work for you are sourcepath and classpath.
I developed two .jar for my application (LOG.jar and STRING.jar).
I use these jar, with import in .java :
import LOG.CLog
import STRING.CString
It's OK. But .jar is increasing in my project, so I would like to create only one .jar which includes all .jar developed.
So I tried this by creating the only one .jar (named TOOLS.jar) :
jar.exe cvmf MANIFEST.MF TOOLS.jar TOOLS\LOG.jar TOOLS\STRING.jar
But, if I put only TOOLS.jar file in my application compilation (Java build path in Eclipse), I get error when I want to import :
import TOOLS.LOG.CLog
This import cannot be resolved.
And In Eclipse "referenced libraries", I see package PXTOOLS which includes both STRING.jar and LOG.jar, but I don't see STRING and LOG package !
How can I fix it ?
I use these jar, with import in .java
:
import LOG.CLog import STRING.CString
You import classes by qualifying them with package names; not directly from JAR files. I hope your package is called LOG and class is CLog here (though it's a bad naming convention to have uppercase package names)
Secondly, merging JAR files into one isn't recommended. It's best to keep them separate. If at all you did want to merge, you must ensure that you extract all the class files first and then merge.
You cannot include jars in a jar file. If you want to deliver just one jar, check out tools like One-Jar. They can package a working jar for you.
I use these jar, with import in .java :
import LOG.CLog import STRING.CString
You import classes by qualifying them with package names; not directly from JAR files. I hope your package is called LOG and class is CLog here (though it's a bad naming convention to have uppercase package names)
Secondly, merging JAR files into one isn't recommended. It's best to keep them separate. If at all you did want to merge, you must ensure that you extract all the class files first and then merge.
Yes, you can do so. Have a look here - Build Java entire project jar using JDeveloper