how to compile java with multiple files using command line - java

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.

Related

How can I use the classes from a jar file in another files, different directory?

Im new to Java and I wanted to use the classes from a jar file, that I've created, in a Main class outside, but when I try to compile from the terminal It doesnt find these classes. I dont know if im writing wrong the import statement, or im setting wrong the classpaths.
The jar file full address :
/home/cristian/Escritorio/The\ complete\ Java\ developer\ course/Sección\ 11/Packages/ThejarFile/example/game/myJarFile.jar
The Main class full address :
/home/cristian/Escritorio/The\ complete\ Java\ developer\ course/Sección\ 11/Packages/com/cristian/example/Main.java
This is the Main class where I want to use these classes:
package com.cristian.example;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import ThejarFile.example.game.Player;
import ThejarFile.example.game.Monster;
import ThejarFile.example.game.ISaveable;
public class Main {
....
I've tried this:
~/Escritorio/The complete Java developer course/Sección 11/Packages$ javac -cp .:ThejarFile/example/game/myJarFile.jar com/cristian/example/Main.java
And this :
~/Escritorio/The complete Java developer course/Sección 11/Packages$ javac -cp .:/home/cristian/Escritorio/The\ complete\ Java\ developer\ course/Sección\ 11/Packages/ThejarFile/example/game/myJarFile.jar com/cristian/example/Main.java
The javac is for compiling and when compiling you refer to the file path with "/"(Windows) or "\"(Unix-Based) and the .java file suffix:
javac com/cristian/example/Main.java
The java is for executing an already compiled class. You refer to the .class compiled file, using the "." without the suffix .class file:
java -cp .:ThejarFile/example/game/myJarFile.jar com.cristian.example.Main

Can `java -cp` report the jar or class file which it finds for each package imported in an executable class?

When using java -cp path1:path2:path3 SomeClass to execute a class, is it possible to make java report the jar or class filename and pathname which it finds for each package imported in the class?
No ... and partly yes.
No because "importing" is a compile time concept only. There is no need to import a class in order to use it, and an import statement doesn't actually mean that the class is actually used. Also, you don't import a package: you import classes from a package, or static members from a class.
Partly yes because the java -verbose:class option will log each class that gets loaded by the JVM.
And if you want to statically find all of the other classes that are directly referenced by a given class it is possible to get this by analyzing the classes .class file.

import local library error in java

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.

java package does not exist and bad source file

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.

javac: package not found error

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.

Categories

Resources