trying to compile after using eclipse and having import problems - java

hi
i'm using ubuntu and i have a little project with several packages i wrote in eclipse.
when i am trying to compile (run javac) from the terminal i get numerous errors like:
Writeable.java:14: cannot find symbol
symbol : class IllegalFilterArgumentException
location: class oop.ex1.filters.Writeable
throw new IllegalFilterArgumentException();
now in eclipse everything was fine. i was told its because i need to run javac from a directory that can see all java files in my project, but it didn't work. any ideas? thanks

If you are using packages (as you are, from the error message), you must arrange your source files in package structure, and then call javac from the root of this structure. For example this way:
root directory
oop
ex1
filters
Writeable.java
IllegalFilterArgumentException.java
Then you'll call it this way:
javac oop/ex1/filters/Writable.java
Alternatively to "calling from the root directory" you could pass this root directory as an option to javac:
javac -sourcepath "root directory" oop/ex1/filters/Writable.java
(You may also want to give other options to javac, look at its help page.)

Related

Java command to run the main class

I have been away from Java stuff for quite sometime, need some help about basic stuff.
I have the following project structure :
There are no manifest files etc, its just a raw folder structure.
I wanted to know command to compile these java classes from root folder Data Structures - Java and command to execute compiled classes.
I did try
javac -d build com.codesuman.datastructures.Main
This worked first time but failed in next attempt.
Thanks in advance.
I don't see how that could work, even a single time :
javac -d build com.codesuman.datastructures.Main
In javac, the last argument is "source files". It has to specify location of java source to compile in terms of filesystem location : that is file or directory.
But that com.codesuman.datastructures refers to a java package. Something like that is expected : com/codesuman/datastructures/Main.java.
So, to compile that class in the build directory, do that :
javac -d build com/codesuman/datastructures/Main.java
But if Main.java relies on other classes, which looks possible according to your snapshot, you also need to compile these classes.
So a more idiomatic approach in this case is :
javac -d build com/codesuman/datastructures/*.java
But beware the subfolders are not compiled.
Main class seems to be inside linear folder
This should do the job javac -d build com.codesuman.datastructures.linear.Main

Compiling and running a java program with complex file structure from the commandline

Trying to compile and run my java program from the commandline that is set up a bit weird. The file structure is as follows:
[ROOT]/
|
|____libs/
| |____myExtraJar.jar
|
|____src/
|____main/
|____com/
|____example/
|____myClass.java
The package is defined at the top of the java file as
package com.example;
I am able to compile the program fine (I think) while in the root folder, using
javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java
I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. However, I can't find any way to run the compiled program. Running
java src/main/com/example/myClass
results in the message
Error: Could not find or load main class src.main.com.example.myClass
Any help would be appreciated.
You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. Like,
java -cp "libs/myExtraJar.jar:src/main" com.example.myClass
Elliot is right. More precisely, you need to add the build directory to your classpath. It is the directory containing your *.class files, and is sometimes named target/.
$ java -cp "target:lib/myExtraJar.jar" com.example.myClass
Moreover, src/main/com/example/myClass should be com.example.myClass, which is the fully-qualified class name. See http://www.manpagez.com/man/1/java/ for details of the java command.

java compiling error "could not find or load main class main.java"

I understand there are a lot of threads similar to this one, but I couldn't find the one that solved my problem. Following this instruction I was able to get java in terminal and be able to compile. I am able to "javac main.java" with no errors, but when I "java main.java", it simply says it could not find or load main class main.java. I believe that my classpath is wrong but i'm not entirely sure how to fix this either. This is what comes out when I type in echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/taka/.rvm/bin
and when I type echo $CLASSPATH it doesn't show anything.
I have also tried java -cp ./ main.java as that seemed to have worked when I compiled and ran HelloWorld.java
If your javac is successful then update your classpath environment variable and add current directory i.e. . in the classpath, then run the java as below:
java main
Please note: There is no .java extension as you need to run .class file(which was generated after javac) that also without mentioning the extension. Java uses generated class files to execute not the original source files.
main.java java is your source code . you cant run java source without compile. For compilation you should use javac command. After that it will create a main.class file which can understand by interpreter which is java.
So you to run your class use java main or java main.class

compiling multiple java classes in linux

i used netbeans to code the classes and they are all included in a package but when i try to compile the application class in linux it spits out errors on class definitions for the classes i am working with. points at the class names for the objects and says "cannot find symbol" i need help!!!
use javac -sourcepath < your source code path >
Better check -help option as it mostly solve your problems
cd to the directory containing your package then run:
javac -classpath . your_package_name/*
I'm not a Java guru, but I have a small java project that I developed years ago and have recently ported to compile with javac on Linux.
I got this to work in two different ways:
Created a single Java source file that held all of my classes
Put each of my classes in a separate file but all in the same directory
In each case, I can compile and run with the following:
javac *.java && java name_of_main_class
Notice that I did not specify a "-classpath" option when I compiled. I guess this works because I have not used a directory substructure or created a package. If you are dealing with those issues, this page appears to have some examples that may help you: Help with packages in java - import does not work
A key thing to understand about Java packages: They correspond to subdirectories where the classes are defined (or to JAR files which just bundle and compress those subdirectories into a single file). Therefore, anytime you specify the package keyword in your source, you need to make sure that the source files (and the class files) are distributed to subdirectories correspondingly. The -classpath option to javac may provide a workaround when subdirectory structures do not exactly match what is specified by the package keyword.
If you built the project using NetBeans, you can use Ant to build the project on command line. NetBeans generate Ant Build script.
just cd into the directory where the project is located then type 'ant'
it should build the project for you automagically

Java can't find class errors in compilation

I am trying to compile and run my java application from the command prompt but am having some errors. I change to the bin bin folder thus am running from the bin folder.I tried compiling with :
>> javac foe.java
but i get some errors that it can't find some classes that are been referred to by the above main class.
Do i have to compile all classes that the above main class references? and if so how do i do it?
thanks.
javac looks in the classpath for those classes referenced by your programs.
If you are unfamiliar with the classpath concept, please see the appropriate Java Tutorial section: http://download.oracle.com/javase/tutorial/essential/environment/paths.html
In foe.java, what are the import statements at the beginning of that file? If you're using classes that aren't part of the standard Java Runtime (classes whose package name begins with java.) such as classes you made, classes that belong in other libraries (jar files), you must add them to your classpath before compiling, otherwise javac won't be able to find them.
The rule is: if it ain't part of the JRE, you have to be explicit when you compile.
if your main file is in c:\path1\mainfile.java
and referenced java file is in c:\path2\reffile.java
from c:\
java -cp c:\path2 c:\path1\mainfile.java
will compile both your files. (i am assuming you are not using any packages)
to run mainfile.java from c:\
java -cp c:\path1;c:\path2 mainfile

Categories