I have a problem with the java compiler.
The folders of my java project looks like this:
main folder
delivery folder
Box.java
Factory.java
FragileBox.java
Supplier.java
test folder
Test.java
Test2.java
Test3.java
Test4.java
Test5.java
DeliveryTest.java
Clearly I have 2 packages aswell, a delivery package and a test package. It's a school project, so the test package was given, we had to write the delivery package which is tested by the test package.
The DeliveryTest.java looks like this:
import java.util.ArrayList;
import java.util.List;
import test.*;
import delivery.*;
public class DeliveryTest {
public static void main(String[] args) {
List<Test> tests = new ArrayList<Test>();
tests.add(new Test2());
tests.add(new Test3());
tests.add(new Test4());
tests.add(new Test5());
int level = 1;
for (Test test : tests) {
if (test.test()) {
++level;
}
}
System.out.println("Az elert szint: " + level);
}
}
I really don't want to detail the work and the purpose of the code, I don't think it's relevant here.
My problem is that if I compile the DeliveryTest.java as javac DeliveryTest.java, it creates the class files, and successfully compiles, but: after the first compile, when all the class files created I make any changes on delivery package it won't compile again, only the test package, and after compiler finishes the compile it will just return as everything went well. It won't even create the class files again if I remove one of the class files from the delivery folder. However, the test package compiles fine after the first compile, it has no problem with that.
How can I achieve that I can compile the delivery package aswell after the first compile?
Thanks for any help
The Java compiler doesn't look around for files to compile. If you call it from the command line, you better use a call that names all Java files in their folders. Assuming that the main folder is your working directory,
javac DeliveryTest.java delivery/*.java test/*.java
If you don't change files in the test package you may not have to use the last parameter.
Related
I want to create a single jar file from multiple packages. I have created the jar using below command but when I'm importing it to a project as a dependency it is not working.
jar cfe output/jar/my-java.jar Main src/pkg1/pkg0/*.class src/pkg1/*.class src/pkg2/*.class
My project structure is something like below structure
src
pkg1
A.java
B.JAVA
pkg0
E.java
pkg2
C.java
D.java
My Example code is something like
import pkg1.A;
public class Main {
public static void main(String[] args) {
A.printMe("Hello World");
}
}
error that I'm getting is:
java pkg1 not exist
But in the editor(IntelliJ), it is not showing errors and also i'm able to import class but not package.
import pkg1: showing red means error in the editor
import pkg1.A: not showing red means no error in the editor
Note: I don't want to use maven.
An unzip -t something.jar shows the actual file structure of the jar file (zip). It is the same as the class structure of it (except that instead "/", a "." is the separator).
In your case, the problem will be that src will be on the top level, and not pkg1. Either import src.pkg1 (very dirty), or play a little bit more with the directories / jar flags.
just call your method it automatic get path if you correctly put your jar in your current project.`project 1
pkg com.test.demo
class test{
public static void m1(){
System.out.println("project 1 in method 1 );
}
}
in project 2 put jar of project 1
pkg com.test.demo
class Test1{
public static void main(String...){
System.out.println(test.m1())
}
}
At school we use a program doctorjava for coding. Now is it possible to import a class with custom methods that is located in a whole other script and if it is how can you reference it?
edit: I know how to import java classes and packages and was just wondering if it's possible for self made scripts with classes and methods to be imported/referenced like the pre-made ones.
edit 2:
I use drJava to make and compile my scripts. The scripts are save as a .java file and the compiled file is saved as .class .
I have 2 scripts
Mathematics.java
public class Mathematics
{
public static int sum(int a, int b)
{
int result=a+b;
return(result);
}
}
and Test.java
public class Test
{
public static void main(String[] args)
{
int x=12;
int y=36;
//here i want to use the sum method from the mathematics class
}
}
I am new to programming and have only made these to scripts and put them into a folder named test on my desktop. Both scripts are compiled to their respective .class counterpart. What do i need to do now to make the sum method work in the test.java script. I can only use drjava, because this is what we use in school
Is what i want even possible and how to do it?
edit3: the problem was only in me forgetting about the main method in the test.java script. But what if the mathematics script isn't in the same folder
but inside a folder that is inside the test folder
directory tree:
test.java
test.class
methods
Mathematics.java
Mathematics.class
in this case the mathematics script is in a subfolder called methods
what does it need to have changed in the script for it to work if possible?
If the scripts are in the same directory the answer is simple for this example it looks like this
Mathematics.sum(x,y);
If not you are required to create a package by adding this line of code to the top
package methods;
The package name should be the same as your folder name in which it is located
Then simply put this line of code to the top of the script with the main method in
import methods.Mathematics;
methods referring to the package name and Mathematics referring to the .java/.class filename
Be cautious since naming the package/script the same as an already existent one in java may lead to errors
Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}
I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit:
As suggested , have changed the package , and tried to run the same code again . Still getting an error.
The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1. However, the package test1; statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the package statement inside the classes says they are inside the folder test1 which is inside /src. Now every package/path can be resolved.
And you shouldn't import things that are in the same package.
First of all as your package name is test you must keep your class and the interface in a folder named test.
Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination
Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.
Then try opening the command prompt in g:/
then type the following commands
for compiling
javac test/Car.java
and for executing
java test.Car
Though you may get Error: Main method not found in class test.Car
as your class does not contain main mathod
You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.
For make this compile you have to specify fully (again Fully) qualified name of package during compilation.
For Example
If you class is in a.b.test package compile it like this
javac a/b/test/Car.java
First compile MotorVehicle as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY> should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY> should be C:/sourcecode
Update
Removing the import test1.MotorVehicle will also fix the issue.
After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error
I have the following directory structure:
A.java
a
โโโ A.java
A.java:
import a.*;
class A {
public static void main(String[] args) {
System.out.println("Hello Human");
}
}
a/A.java:
package a;
public class A {}
And it compiles and runs:
$ javac A.java && java A
Hello Human
But when I remove a/A.java (but keep the folder a), I can no longer compile it:
$ rm a/A.java
$ javac A.java && java A
A.java:1: package a does not exist
import a.*;
^
1 error
Why?
You might say that the package a is not observable:
A package is observable if and only if either:
A compilation unit containing a declaration of the package is observable (ยง7.3).
A subpackage of the package is observable.
source: Observability of a Package
This is your problem right here:
import a.*;
It's looking for something that doesn't exist, since that package is no longer there.
Remove that line from A.java and it should work fine.
The reason it can't find the package is because there is no compilable classes within the folder, and so it isn't recognised as a java package.
One option for creating an empty package, if you do need one, would be to specify a package-info.java file, which is generally used as a javadoc for packages.