Compiling java package - java

My project contains three source files that I have created and one testing file (Homework1.java). The directory structure looks like this:
ija/ija2016/homework1/HomeWork1.java
ija/ija2016/homework1/cardpack/Card.java
ija/ija2016/homework1/cardpack/CardDeck.java
ija/ija2016/homework1/cardpack/CardStack.java
The HomeWork1.java file contains the main method, some tests and also imports the other three files:
import ija.ija2016.homework1.cardpack.Card;
import ija.ija2016.homework1.cardpack.CardDeck;
import ija.ija2016.homework1.cardpack.CardStack;
Now I am able to compile the Card.java, CardStack.java and CardStack.java, but I am only able to do so directly from the /cardpack/ directory. When I try to compile them from anywhere else, then the CardDeck and CardStack classes don't recognize the Card symbol.
My question is, how do I compile the project as a whole? Should the three source files that I have created have import package in the header (the CardStack and CardDeck use the Card class)?

in your root folder, try to run:
$javac -cp . ija/ija2016/homework1/HomeWork1.java
then you can run your program
$java -cp . ija/ija2016/homework1/HomeWork1
-cp/-classpath: defines your classpath.

Related

How do you specify the classpath when running the javac command?

I'm trying to import Bar.java into Foo.java, where my directory is structured like this:
.
└─bar
└─Bar.java
└─foo
└─Foo.java
So I create Bar.java:
package bar;
public class Bar {}
and Foo.java:
import bar.Bar;
public class Foo {}
I run javac from the foo directory:
javac Foo.java -cp ../bar
and receive the error:
Foo.java:1: error: package bar does not exist
My understanding was that the -cp option should be used to point towards the required class (Bar.java). Could someone shed some light on what I'm doing wrong?
Package bar does not exist because the prompt or terminal "working directory" is inside foo.
Easiest, make a folder java2 and put both folders in it and use use the storage folder as "system working directory" for terminal or prompt,
However, normal "package" hierarchies compiled together are both in a top level starting directory tree.
E.g.
/com/foo/ and /com/bar
So your package would look com.foo and com.bar
Put the com folder in the java2(simply a name you can use any name for the storage folder)folder.
Your import statement at the top of the class files should have the com directory added to import the other file.
Just command inside java2 folder
javac /com/foo/Foo.java
-cp is for specifying to include dependent .jar libraries for the compilation.
If Foo does not import Bar then it will be required to be separately compiled the same way as Foo.
When framework API libraries are made its the user program requires to mark a starting point to unload the jar or look for a package folder and is traditionally "com", however, it does not distinguish which API until a folder lower so your package folder really should be structured
/com/mypersonalapi/foo
/com/mypersonalapi/bar
com.mypersonalapi.foo
com.mypersonalapi.bar
You need to give like this:
javac Foo.java -cp ..
When you give classpath as shown in your question, it expects the package "bar" under the folder "bar"; which is not intended.

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

bad class file using javac

Some background: I'm a student just learning Java, and usually the professor handles making sure our assignments have the right class path. However, the whole semester I've been plagued by the same problem, and I still don't understand what's going wrong.
As an example, I have two files, MyProgram.java located in the folder "MyProject" and MyProgramTest.java located in the folder "ClassProject", which also contains "MyProject". MyProgramTest creates a MyProgram object and lets you test its functionality.
MyProgramTest has the line import MyProject.MyProgram; The compiling instructions my instructor gives is to use javac MyProject/*.java while in "ClassProject" which works fine. Then, we are to use javac MyProgramTest.java in the same directory. However, the compiler claims:
import MyProject.MyProgram;
bad class file: .\MyProject\MyProgram.class
class file contains wrong class: MyProgram
Please remove or make sure it appears in the correct subdirectory of the classpath.
So I don't quite understand why this is happening. MyProgram is in the MyProject directory, and that directory is in the folder I'm in. Since the instructor uses this exact method to compile these programs, I keep getting screwed since mine never compile correctly. Any idea what I'm doing wrong, or how I can fix the file to compile this way without changing the structure of the directories?
Ensure this:
The MyProgram.java file should contain this line at the top of the file:
package MyProject;
Compile MyProgram.java from the ClassProject folder:
javac -d . MyProject/*.java
Then Compile MyProgramTest.java from the same folder:
javac MyProgramTest.java
This will create the class files correctly in the appropriate folder structure.
This should solve your problem.
Hope this helps!

Compiling 3 files not in the same package

I have 3 *.Java files. Two of the three have the statement package com.osama.temp and the main class does not have this statement. It only imports the 2 other classes like import com.osama.temp.List.
I don't know how to compile it with the Command line console .. please Help!
In the directory where the main class lives create a subdirectory structure like com/osama/temp/ and put the other 2 .java files in there. Then you should be able to compile it with javac Main.java

javac is unable to find import

I am having trouble compiling a file with javac.
My command to compile is:
C:\Work>javac -classpath "%CLASSPATH%;C:\Users\Me\Downloads\asm-4.1-bin\asm-4.1\src" wDump.java
Results in:
wDump.java:3: error: package org.objectweb.asm.attrs does not exist
import org.objectweb.asm.attrs.*;
^
1 error
That package most certainly DOES exist. Its in the same directory tree as all the other ASM imports. attrs is an empty package that contains a single HTML document.
wDump.java file also contains
import org.objectweb.asm.*;
Which is found by the compiler without any problems.
If I comment the problem import out(org.objectweb.asm.attrs.*),
the file compiles successfully. I should also note that the Eclipse compiler compiles this file successfully.
You might need sourcepath not the classpath
use the following:
javac \
-sourcepath src \
-your_jar_path/your.jar:. \
your_class_path/your.java \
-d classes
If your code is compiling when commenting out the import line, then you don't need it and can remove it.
If you want to use it, check that attrs is the correct name of the package, as it seems that import org.objectweb.asm.*; is working. And check that attrs is effectively a package and that the folder is physically existing inside of asm folder. You can also check that the class files are present inside the attrs folder.
The attrs folder in the asm source zip contains only an html page without any class inside. Try to remove this folder (you can save it before in other directory if you are not sure) and to generate again the imports through the ASMifier tool. If folder doesn't exist, maybe the tool won't generate automatically the import.
For your information, you shouldn't use external source code to compile your code, except if you are modifying these external sources. Use instead the jar files, added to your classpath, to compile your code. In the asm-all-4.1 jar file, there is no attrs folder.

Categories

Resources