Compiling 3 files not in the same package - java

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

Related

Error: Could not find or load main class (java.lang.NoClassDefFoundError) Ubuntu 18.04

Using the ls commmand, I can clearly see both the .java file and the .class file of my main class created from compilation. However, when I try to run it, the Ubuntu terminal says that the class was not found. Here's a screenshot of my terminal
And here's a snippet of my main class
Any answers?
You haved a class inside package. So you should run your class in /home/se folder and use command java cs3421_emul.cs3421_emul
Since your classes are in a package your compiled classes should in the proper folder structure.
To compile the classes to proper package folders use -d . switch while compiling.
javac -d . *.java
The above command will create folders based on the package of the classes. for example you class3421_emul has a package class3421_emul so it will be compiled to class3421_emul folder.
Then run the class using java class3421_emul.class3421_emul

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 java package

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.

Using javac with import

I try to compile this code:
package edu;
import java.io.*;
public class Main { ... }
with javac called from command line. I know that I need to do it this way:
javac -classpath /lib/* Main.java
and put .jar file with the 'java.io.*' classes in 'lib' folder in my project's directory.
Is my javac command correct - especially path to '/lib/*'?
How do I find the desired .jar file(s) so that I can copy them to my project's lib directory?
package java.io is part of core java, you do not have to put anything extra into your classpath

How can I include packages(folders) in the compilation of a java project through terminal?

So, I want to compile a java benchmark.
I am working inside the folder /home/username/Tools/myTool/folder2.
I am compiling with javac -cp /home/username/Tools/appv1.0/ *.java
folder1 compile sucesfully because it did not have any dependencies on packages.
Right inside folder 2, that I have the problem, there are 5 folders residing as packages(having some java classes), but the compiler can not recognize them, so I need to put that explicitly.
And I keep getting errors like this
JClass1.java:22: error: package crypt does not exist
import crypt.*;
^
JClass2.java:23: error: package series does not exist
import series.*;
^
So, how can I direct the compiler towards those packages?
Thanks in advance.
You must write the command to compile as:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/YOUR_FILE_NAME.java
As a result bin/com/example/Application.class file should be created. If Application.java uses other classes from the project, they all should be automatically compiled and put into corresponding folders.
For more info.
The anwser provided by yogx was the right one after all!!
Problem solved.
javac -cp dir1/*:dir2/* MainClass.java
Thank you all!

Categories

Resources