I try to use JavaCompiler to compile source code.
class A{int i;};
class B extends A{i = 5;};
The problem is even if they are in the same folder, When compiling class B, JavaCompiler still can't find Class A.
So, I am wondering the problem is I didn't add the path of the folder to classPath.
I don't know how to do it in java code, So didn't give it a shot.
Thanks for any help.
You need to set the class path for the compile task.
Have a look at the answer over here:
How to set classpath when I use javax.tools.JavaCompiler compile the source?
another point of view would be to generate directly the bytecode using one of the famous tools for such task like ASM, JavaAssist, SERP or any other one.....
It could be a very good way to avoid :
- path problems
- to have a finer control on the process (if you have javac errors you will be obliged to parse the stream to raise thme into your application)
- improve the whole process performance
But it adds some complexity...
Like often it's a trade off
extend the classpath to the current directoy.
You can do that via the -classpath option or the CLASSPATH variable.
-claspath=.
or
CLASSPATH=.
Related
I have deploy my java code on AWS cloud, when I compile it on windows through terminal, I just have to use the command.
javac mainApp.java
it automatically create .class files of all other classes, lets say I have another class
class newProcessClass
who's variable is in mainApp.java, on EC2 when I compile it, it is giving me the error
error: could not find this symbol
newProcessClass npc = new newProcessClass();
same for the other classes. how can I compile it, and run it.
According to your description, I think that might be caused by classpath issue,
So please make sure current path(.) is under your classpath.
You can do this by type the follow into your terminal(pay attention to the little dot please!)
export CLASSPATH=.;$CLASSPATH
If you would like to persistent this setting and avoid set this everytime, you'd better add it to your .bashrc file.
And then when you run the class via java command, please also specify classpath as below
java -cp . mainApp
I recommend use Maven for the life cycle for application java and when you compiled the mainApp.java this action has that compile all file. you can get me more information of error stack?. For other side I think that is best that you use Maven. Best regards
Maybe this question may be splitting hairs, but when I compile a file from command line like :
javac MyClass.java
then afterward I cannot run it by saying
java MyClass.class
I have to call:
java MyClass
What is the motivation for this notation?
Because you run a class on the classpath, which may be contained inside a jar for example. You couldn't use your syntax in that case.
Java compiler needs a compilation unit; this is by default (at least) a java source file, with the whole of classes defined in it and its dependencies.
Java interpreter (the jvm) needs a single class with a main method as entry point of the execution - it must start somewhere.
You'd have to ask Sun (now Oracle) for the development history, but I do want to point out that for folks who are just using Java rather than developing Java, "java DoSomething" is easier to remember, and to type, than "java DoSomething.class"
There is no way to run a Java program that is not a class. For that reason, there is no reason to mandate typing the ".class". You might also invoke a class from within a JAR on your path, or directly, but it's still instantiating a class (possibly a "default" class from the Manifest).
Because the name of the class is MyClass and not MyClass.class. And when running java you specify the CLASS NAME and not the PATH to the actual compiled file.
For more in depth knowledge I guess Sun & Oracle will have to answer :)
Imagine that you have a class named package and you have a class named Class, in a package named package,
--CurrentFolder
--package
Class.class
package.class
so executing java package.class may lead to an undecidability to the compiler!
I found a traffic light simulator called Green Light District
the file I download included and explanation of how to build the project
and it says the following
Extract the archive to a certain directory,
In that dir do javac gld/*.java
In that dir do java gld.GLDSim
I know * means all but...
first I want to know am I able to compile something like *.java
this is a link to the simulator , if some one can tell how to compile it , it would be really nice
http://sourceforge.net/projects/stoplicht/
The issue occurs due to the fact that "enum" became reserved word in Java 1.5+, so it can no longer be used as a name for variables/methods/classes, but GLD uses enum as a name for some variables.
Once the name is changed - project will compile.
Alternatively one can try to compile it with compiler setting source version 1.4 - this should probably also work, but I suggest to simply rename all variables named enum.
Basically, all the code is in the folder called gld. You need to compile all the .java files inside it. After that, you run the main class gld.GLDSim.
Had the same problem.
You should use Java 1.4, this simulator is quite old, hence uses an old version of Java and some preserved words in more advanced Java versions.
I'm new to java development, I just want to use javac for my build system. I'm using java to add a feature to a program someone else wrote, specifically involving GeoTiff images.
I found a class online that I would like to use, however I'm having trouble building the class, no matter what I do I get this message:
javac GeoTiffIIOMetadataAdapter.java
GeoTiffIIOMetadataAdapter.java:11: package com.sun.media.imageio.plugins.tiff does not exist
import com.sun.media.imageio.plugins.tiff.GeoTIFFTagSet;
I'm on RHEL5, so I installed the package I thought I needed, jai-imageio-core.x86_64. But the problem persists. I think that I'm not setting some variable corrently (like -sourcepath or something). I would appreciate any help.
You need to include the jar with -cp or -classpath.
So your compile would be like java -cp "<location to jai_imageio-1.1.jar>" <your java class> .
I think you need this jar file.
You can read more about javac here.
Find out where the package installed the jar file with the class you want to import, and add it to the javac commandline in the -classpath. (You then also need to include it in the classpath when your plugin runs; how to do that may depend on the program it plugs into).
I think that I'm not setting some variable correctly (like -sourcepath or something)
This tutorial briefly introduces the usage of environment variables in Java: PATH and CLASSPATH
This one seems to be the most popular answer to various classpath related questions I've seen at online forums: Setting the class path.
To avoid "blind recommendation" I quickly skimmed through it before adding to this answer and, well... it really covers most of what one needs to know to deal with classpath. Pretty good; the reason why I didn't look into it before is that there always has been some guru nearby who explained stuff to me.
I have a large number of classes in a project, and I would like to compile all of them from a script. The thing is, the classes should be compiled in a certain order, for example: I have a class named A which depends on a class named B. Let's say class B depends on a class named C. In order for me to compile class A, I would have to compile first B and C.
Is there some tool I could use to establish the compile order of the classes, so that I don't have to parse each class and determine this myself? I would preffer that the tool can save a file with the order of the files to be compiled, so that I could parse that from my script.
Thanks !
If you compile all of them at the same time (in the same javac invocation), you do not need to do anything of the sort you are describing. javac is smart enough to compile all files you give to it at the same time, so it doesn't have any problem with out-of-order compilation.
The Java compiler (Javac) already builds up a dependency list of all the class files you need to compile. The real dependency here is between packages - not individual java files in the same package (this is automatically taken care of by the compiler).
Use a tool like Ant or Maven to specify and compile all the files in various packages and produce your final distribution.
If you are using an IDE like NetBeans, it automatically does this for you. Alternatively, if use a tool like JDepend
These kinds of DAG ordering problems are usually solved with topological sorting. See Wikipedia for a description. I don't know if there is a tool such as the one you are looking for, but implementing it yourself is should not be that difficult.