Javac simple package (JavaBib) - java

Dear Java Programmers,
JavaBib (http://code.google.com/p/javabib/) is a simple open source java package for which I would like to generate bytecode.
The source can also be obtained by version control via
svn checkout http://javabib.googlecode.com/svn/trunk/ javabib-read-only
Although it consists of only a few java files, I am not able to compile it using
javac *.java
I get
cannot find symbol
error messages for nearly all of the files.
The source files of the dom package all reside in one folder. Therefore, I think it should not be a problem with the classpath. I have tried different values for it nonetheless.
There are additional source files in other directories. I tried copying them all in one directory and run javac in this directory, but javac was complaining about the same imports.
Unfortunately, I have hardly any experience with java and can't fix the problem myself.
Therefore, I would be grateful for hints towards additional diagnostics in order to pin down the problem.
I use Ubuntu 12.04 as operating system and haven't set any classpath or sourcepath variable. At least javac seems to find imports like "java.util.list".
Thanks in advance for your help.

Related

some basic concepts regarding JAVA and Linux: PATH variable in LINUX and class location by JVM

I am new to Java programming and Linux environment. And I am finding it difficult to understand few things about what is classpath, how does JVM locate classes, and JAVA API and many other things.
For example , today I wrote two simple classes 1)employee and 2) employeetest and placed them in the same folder.
(employeetest has the "main" method and uses employee in its code.)
I compiled employeetest and then executed it using "javac" command.
I saw that , employee.class was also added to the folder. So does this mean that JVM automatically compiles all those files that are required for execution?
Then i placed the employee class outside the current directory , and then tried to execute employeetest. Now I got an error regarding ClassNotFound!!
why is it so? why didn't JVM search for the employee class in other directories?
The directory where I placed employee is also on my classpath or "PATH" in my linux?
technically it should search for other directories also that are there in the PATH ?
correct me if I am wrong, because I am reading so many things on internet, I am not able to figure these concepts out clearly?
SO where does JVM search for the classes? In the same directory where the class with "main" is located?
On my machine when i do echo $JAVA_HOME nothing prints. but still my java and javac commands execute properly? why is it so? what is the meaning of $JAVA_HOME? where is JDK located? what are its functions?
PATH and classpath are two very, very different things. PATH is a machine specific environment variable that the operating system (a Linux distribution, in this case, but Windows uses the same environment variable) uses to find executables. Executables include binary programs and some script files in Linux. Unless you are specifying the full, absolute path of javac or javac is in the current directory, PATH is how Linux finding the javac binary.
Class path, on the other hand, is Java specific. It can be set as an environment variable CLASSPATH or as an argument to the java executable like so:
java -classpath /some/dir:/some/other/dir myprogram
This is the set of directories where the JVM looks for class files OR packages (folders with a particular structure that contain class files), aside from the built in API.
Yes, the Java compiler does compile dependent source files if it can find them and determines that the matching class file is missing or out of date. The compiler will first search on the "sourcepath" argument if specified, and it will search on the class path as well. You may find it helpful to read over the command's documentation: javac. (That's for version 6. I couldn't find version 7, but I think all that applies.) Here is the documentation for java.
The JDK's and JVM's locations depend on where they were installed. Try which javac to find where the JDK is and which java to find the runtime; this will show where Linux is finding those executables (which it is probably doing via PATH).
I spent quite a bit of time rooting around through Java's documentation in my college career, and I gleaned a lot from it. You may find rooting around a bit yourself worthwhile. Here's the link: http://docs.oracle.com/javase/7/docs/.
Here are few basics of java/java compiler
You write java code ---JVM loads class file , the class file is the bytecode that actually makes java more portable(platform independent)
Your situation
You compiled the source inside a folder say "foo"
so for you to be able to compile it from anywhere you should provide a path to the class file
so
javac -classpath somepathtothatfile
use the export command to set the path to that location where you now have the class file then that error will be removed
like export CLASSPATH="pathtosomelocation"
Jvm looks for the files inside its bin directory in windows its -c://programfiles/java/jdk(version)/bin/
in linux
/usr/lib/jvm/somejavaversion/bin
check it out
TWO THINGS FOR JAVA DEVELOPMENT
JRE-this is just the runtime things ,only to run
JDK-that you need to develop code,to get the access to API you need .

Imports, jars, and heart Ache

first off let me start by saying I am completely new to Java, but to give you an idea of how new; I started reading lots of books, examples and so forth and began programming Java using Eclipse about 2 months ago. However, I found a really cool bit of advise about using notepad and the terminal to program instead. Kinda crazy for a newbie to go the hard route, but I love a challenge and I'm serious about learning.
So, In Eclipse I had a really good grasp of how to import, add jars compile etc. When I started using pico and using the terminal (I'm running ubuntu) to compile all went really well, until I wanted to use packages. I've spent two days pulling my hair out because no matter what I do I can't figure it out.
I'm trying to use the acm.jar (which I have many times in Eclipse) however I'm completely lost on how to use it when compiling from the javac in terminal.
So what I'm asking for, is for someone to explain the process getting my jar file to work.
All I'm using to create my java programs is the pico (or notepad) and the javac in the terminal.
To compile and run a java class using external libraries, you have to add that library to the classpath. The classpath is the set of places where the java compiler and the JVM look to find any external libraries/classes that it needs during the process of compiling/executing.
Setting the classpath can be done in 2 ways:
Set an environment variable called CLASSPATH
Set it when your run javac/java
Setting the classpath when running javac/java is done like this:
javac -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass.java
To run:
java -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass
: is used as a separator on Linux, for windows use ;
Assuming your source files are in src. Assuming you want your compiled classes to be in classes. Assuming your source files reference classes that are in lib/acm.jar:
javac -cp classes:lib/acm.jar -d classes src/com/foo/bar/MyClass.java
will compile the class com.foo.bar.MyClass and put the generated MyClass.class file in classes/com/foo/bar.
You need the acm.jar file in the classpath. That's what the -cp lib/acm.jar option does. You also need classes in the classpath, because MyClass probably references other classes that you have already compiled and that are in your classes directory.
To run your class, it has to be in the classpath, and acm.jar as well:
java -cp classes:lib/acm.jar com.foo.bar.MyClass
As you see, the classpath contains jar files, and directories containing the folder hierarchy which matches the package hierarchy.
I wouldn't use javac from the command line directly, though. Try using a real build tool, that will build all your classes at once, like Gradle or Ant. Maven is also very popular, but I hate it with passion.

Creating a .h file using javah, getting error "Could not find class file for NativeDemo"

I have researched several examples similar to this issue but i have yet to find one that is the solution to my problem. I am simply trying to do my first program using Native methods. I don't have the program stored in a hiearchy of packages because i tried to keep it as simple as possible for my first example. Here is how everything is stored:
I Have one class called NativeDemo. It is stored in C:/JavaFiles/demo/. I compiled the program and the .class file is stored in this same folder. When I try to invoke the javah command on this program it tells me the class file can't be found. Can you please tell me what I am doing wrong?
My javah command is stored in my JDK and is stored as follows: C:/jdk1.0.7_04/bin/
On the command line from the command prompt I type:
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -jni NativeDemo
also tried C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah.exe -jni NativeDemo
When I do this it tells me the class file can't be found. This is the same path used when compiled and it found the file, compiled, and created the .class file with no issues.
Please help. Thanks.
You probably need to supply a -classpath parameter on the command-line to javah to set the classpath. (The -classpath parameter for javah behaves the same as it does for other Java tools; e.g. java, javac, javap and so on. If you don't understand classpaths, read this page and this page.)
As the solution was never added I thought I'd contribute the solution I found to this problem. It wasn't really a solution as such as it was 100% user error!.
In what sounds like a similar situation I wanted to create a quick small project to demonstrate the use of JNI. As this was designed to be a simple exercise I didn't bother with an IDE and simply used vi to write the code and javac to compile it.
e.g
myclass.java (fully qualified class name is mypackage.myclass)
javac myclass.java
The above command outputs myclass.class to the current directory. I now have myclass.java & myclass.class in my current working directory.
Running javah mypackage.myclass results in the error described.
The problem here is my use of javac, I wrote the compiled class to the current directory, javah however is looking for it at "/mypackage/myclass.class".
Silly little problem with a silly little answer but I was quite annoyed at wasting 15 minutes on this today so hopefully I can save someone else the same pain (and yes I know I should have spotted it sooner and may have had I not just had an entertaining few hours finding System.load discrepancies between hotspot and gnuj java implementations, so sadly I wasn't exactly trusting my tools :( !! ).
You have to use fully qualified name for the class.
The syntax for javah is javah [options] classes. classes are specified with their fully qualified names. So, in your case if ur package is demo then, the command would be,
C:\JavaFiles\demo>C:\jdk1.7.0_04\bin\javah -classpath . demo.NativeDemo
Classpath is . because, as you mentioned you have .class file in the current directory. And -jni option is not needed, cuz its default.
in android studio 1.5 I had same problem and run above commands but nothing , I explored the app module build folder and there wasn't classes folder in intermediates folder so before run commands make the project, you can do this by Build>Make Project or shortcut key Ctrl+F9, then run above commands
I solved this problem with the following command used in the directory of the eclipse project:
javah -classpath [PROJECT_DIR\bin\classes] class.name

Where do java packages live on a linux system? Package org.json does not exist Error using javac [duplicate]

This question already has answers here:
What is a classpath and how do I set it?
(10 answers)
Closed last year.
I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.
I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).
Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.
So I have a couple questions:
Does anyone know where the org.json
package lives from android sdk
install?
Where might I find a tutorial
explaining these basic concepts
regarding compiling, and javac.
Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as
javac -classpath /path/to/json.jar;. -g YourClass.java
or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.
It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.
Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.
Here's the first thing:
$ echo $CLASSPATH
and you'll see your classpath as it is.
Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run
$ locate json.jar
And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.
Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.
Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)
/src
/org/json/ ... put the json files here
... put your files here
and when you compile, they'll all be included, which will resolve all the dependencies.
Again, the place to look for first steps is that tutorial.
use "java" command instead of "javac"

How does one build the java JRE from source (src.zip in JDK)?

Surprisingly enough I couldn't find the answer to this question.
I am trying to rebuild the java JRE from source. I obtain the java JRE source by extracting the src.zip file in the JDK.
After making any changes I need to make to the JRE, how do I compile the new source back into .java files (after which I can compress it into the rt.jar file).
Thanks.
You have better chances using OpenJDK (the base of Oracle/ Sun's future JDKs).
http://openjdk.java.net/
But what do you want to change actually? Maybe there is a better way...
Some of the Java sources that make up
rt.jar are generated during the build
process, from scripts and other means.
Some of the properties files are also
generated this way, and some of the
properties files are turned into Java
source that also contributes to
rt.jar. So without doing a complete
build first and populating the
'gensrc' directory, you won't have all
the sources that make up rt.jar.
Taken from:
http://www.java.net/forum/topic/jdk/java-se-snapshots-project-feedback/it-possible-just-build-rtjar
So when you say javac on all the java files inside src.zip it won't compile as the dependency graph is broken (missing generated files)
Also have a look at this: Where to get full source code for rt.jar?
If you want to change a number of class, you only need to compile those classes. You don't need to compile the whole JDK unless you intend to replace it.
If you just want to patch it, create a JAR of your changed classes and add this to the boot class path.
After revisiting the question. Javac on any of those files will allow you to rebuild them. Also you don't compile .java files into .java files they become .class files. You can write an ANT build script to handle the heavy work for you.

Categories

Resources