Unable to get javac to automatically recompile a source file - java

I am experimenting with the javac command line options, in order to learn about the -sourcepath and -classpath options. When I run javac, having tried four different command line options for it, I am unable to obtain a recompiled .class file.
Here is my folder structure. Please note that due to testing, the Test.java file is located inside the "bat" folder, which is an admittedly odd location.
projects \ prj1 \ bat \ bat.bat
Test.java
Test.class <--- unable to obtain recompiled file.
src \ Main.java
Main.class
The contents of my two .java test files are:
// Main.java, located in the src folder
class Main {
public static void main(String[] args) {
new Test();
}
}
// Test.java, located in the bat folder
class Test {}
Regarding the execution of javac at the command prompt, here are four options that I have tried. I've run these commands from a batch file called bat.bat, which is located inside the "bat" folder.
javac ..\src\Main.java (no sourcepath, no cp)
javac -sourcepath . ..\src\Main.java (sourcepath)
javac -cp . ..\src\Main.java (cp)
javac -sourcepath . -cp . ..\src\Main.java (sourcepath, cp)
In all of these javac commands above, I am unable to obtain a recompiled .class file, for the Test.java file. Is this because I have not edited the Test.java file, since initially compiling it? Please note that I have no CLASSPATH environment variable set. Thanks.

If class files are already present in the destination / output folder, javac will only recompile the source java file if it has been modified since the date/time of the class file.
If you want to recompile the source files, then first delete the *.class files before calling javac.

Related

Java does not see JAR file in classpath

Not a JAVA Newbie!
I keep getting Error: Could not find or load main class, this was working last week. My Commandline is
java -cp sample.jar com.sample.Myclass
sample.jar exists in current folder, Myclass.class has public static void main(String[] args) method
I have tried
java -cp C:\fullPath\sample.jar com.sample.Myclass
When I add option -verbose:class to the command line above I see all jars in jre\lib but not classes from my sample.jar
Why is java ignoring my -cp /-classpath argument?
JAR file is generated as artifact from IntelliJ and Eclipse->Export JAR as well

How to compile java on unix using -cp

i have 3 files i need to compile
the first a.java compiles fine with
javac a.java
the second requires uses 2 jar files aswell as the file i compiled, a.class. i try to compile the second file with this
javac -cp .:firstLib.jar:secondLib.jar b.java
i just get errors whenever an instance of the a class appears in the b.java file that says cannot find symbol
i have read that unix uses : and windows uses ; and i have read that i need to point it to the directory that contains the files which is why i need the . as the first in my list i pass to -cp. this attempt finds the two libraries but can not find the a.class that i compiled with the previous line.
the third file i want to compile i can not even try to compile as it is dependent on the second
also worth saying this works fine on my windows pc with eclipse im just moving it to my unix server so there should not be any coding errors
i found a fix but still do not know why my original attempts did not work
first i moved all java files to a folder called src
second i moved all jar files to a folder called libs
then i compiled a with
javac -c ../classes a.java
Note: i ran that from the src folder
then i compiled the second with
javac -c ../classes -cp ../classes:../libs/* b.java
and the third file with
javac -c ../classes -cp ../classes:../libs/* c.java
i hope this helps someone else that had the same issue i did

What is the proper entry point, when compiling java to jar?

So, when I write my java file:
public class Program
{
public static void main(String[] args)
{
System.out.println("Serious business logic.");
}
}
Then in windows cmd, I compile this way:
javac Program.java
jar cfe Program.jar Program Program.class
java -jar Program.jar
It's fine, and the result is:
"Serious business logic."
When in Netbeans I create a Project, it adds this line:
package program;
And I can not compile in cmd, only inside the IDE.
I've tried manifest.txt, UTF8 encoding without BOM, plus linebreak at the and of file.
Manifest.txt:
Main-Class: program.Program
"jar cvfm Program.jar Manifest.txt Program.class"
and without manifest.txt, just in cmd program.Program
"jar cfe Program.jar program.Program Program.class"
When I tried to run:
java -jar Program.jar
it results in:
"Error: Could not find or load main class program.Program"
I've already checked the following websites:
http://www.skylit.com/javamethods/faqs/createjar.html
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
and haven't got any idea how to do. Could you please help me?
How do I compile with package keyword? What is the proper entry point?
Thanks!
(ps jre1.8.0_91 ; jdk1.8.0_66 should I use same 32 or 64 bit for both of jre and jdk?)
Make sure when you are compiling your program as a JAR, Program.class is inside of a folder called program. The package keyword that Netbeans has added at the beginning of your script is telling the executable that it is inside of a folder called program. If you are just adding the class file without making sure it is in the correct package (folder), it will not run properly because it does not know where to find it. Your command should be changed to:
jar cvfm Program.jar Manifest.txt program
where program is a folder containing Program.class. Your manifest may be left alone but also needs to be included with compilation.

Unable to compile Java class because the file isn't found

I have file under classes in tomcat...\webapps.....\classes named PropertyExample.java
in classes I have a folder called foo in which I have a class person.java
I am importing that person file in PropertyExample.java
and trying to compile PropertyExample.java this below but its showing an error
C:\>javac -cp .;"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes" PropertyExample.java
javac: file not found: PropertyExample.java
Usage: javac <options> <source files>
use -help for a list of possible options
You execute javac the the folder C:\ (root folder). The .java file is somewhere else.
So it can't be found.
The command: C:\javac Someclass.java works IF and ONLY IF the file Someclass.java is in the folder C:\ you wrote that your file is in ...tomcat/webapps/classes (whatever) so you must do a cd to that dir before calling the javac. like this:
cd c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes
javac -cp . PropertyExample.java
The classpath tells javac where to find class files - not where to find source files. You need to give the path name to your source file (either relative or absolute). For example:
javac -cp "c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes"
"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes\PropertyExample.java"
Of course it would be easier just to change to that directory to start with... or better, to use an IDE or a build system like Ant...

Having a trouble on java.lang.NoClassDefFoundError

I have a test.jar package and a Test.java file in the same folder(it is not in my CLASSPATH).
In the .java file I import some classes that are in the .jar package.
So I go the that folder, use java -classpath test.jar Test.java to compile. Everything looks good. Then I use java Test, it gives me an error:
Exception in thread "main" java.lang.NoClassDefFoundError:......
saying that it cannot find the classes inside the .jar file.
I move the file to Eclipse and add the jar file, it works.
just as you compiled the code using javac -classpath test.jar Test.java you should use the java command to run (add the jar file in the classpath)
java -classpath test.jar Test.java
You must run the java command with the classpath too, same as the compiler.

Categories

Resources