No Resources are loaded when compiling manually with javac - java

I want to manually compile Java-Code with javac. I'm using multiple Images loaded with getClass().getClassLoader().getResource("...") If I run my Code in an IDE everything works fine. But when I'm trying to compile with javac no Resources are loaded. Is there a way to specify the Folder which contains all my Resources? I already tried --add-module but this didn't work either.
My command for compiling looks like the following: javac -d ./out main/ai/*.java main/logic/*.java main/gui/*.java main/network/*.java && java -classpath ./out logic.Launcher

The solution was to just include the path to my Resource Folder in the classpath. I just had to add -classpath path/to/my/res So my working command looks like this: javac -d ./out main/ai/*.java main/logic/*.java main/gui/*.java main/network/*.java && java -classpath ./out:./res logic.Launcher

Related

how to use remote jars in classpath for javax.tools.JavaCompiler

I'm using JavaCompiler to compile .java code.
It works for me while running the class compiled using URLClassLoader to load remote jars.
But not work while compiling.
Tried to use options like
options.addAll(Arrays.asList("-classpath", "https://example.com/example.jar"));
Also tried to use customized JavaFileManager with URLClassLoader.
None of them works.
try below commands, and replace the .jar file with your external .jar file... into the command prompt or terminal.
$ javac -cp ".:./jars/common.jar" helloworld.java
$ java -cp ".:./jars/common.jar" helloworld

How to use javac with jars and packages

I'm trying to run java from the command line, and haven't had to include extra packages with the javac command before and I can't figure out what I'm doing wrong.
I'm running javac -d bin -cp jar1:jar2:...:jarN:PackageName MyClass.java but I'm still getting an error: package PackageName does not exist
I'm using the absolute paths for everything, and I also tried individually listing the java files inside the package but that didn't work either. I'm using a colon since I'm on a mac.
Anyone know what I am doing wrong? Thanks for any help!
I realized that I needed to compile the java files in PackageName before I could compile MyClass.java, which depended on them. So what I needed to do was: javac -d bin -cp jar1:jar2:...:jarN PackageName/*.java and then I could compile MyClass.java with bin added to the classpath as well as the jars.

About java compiling

I programmed two java classes (Coordinate.java and Orienteering.java) using Eclipse. These classes are stored in C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
The package is jp.co.worksap.global.Orienteering. I want to know how can I compile the two files in Windows command line and run it? Orienteering.java is the main class.
First open the command line, then change to the source folder
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
javac -g Orienteering.java Coordinate.java
That should generate two corresponding .class files (with debug symbols). Then move up a few directories and execute java like,
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\
java -cp . jp.co.worksap.global.Orienteering
note alternatively, the javac line could have been javac -O *.java (which would not include debug symbols and would optimize the build, and would compile any other java source files). Also, it is more typical to use a tool like ant, maven, gradle or sbt for java builds.
In Eclipse there is an option to compile it for you. I believe it's:
File -> Export -> Runnable Jar File -> [Select the classes] -> Select Location -> Run the JAR File that gets created in that location.
As far as I know, I think Windows CMD can only compile one Java Class.
Try this:
Goto your source directory using cd command:
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src
Compile your classes using javac
javac jp\co\worksap\global\Coordinate.java jp\co\worksap\global\Orienteering.java
To run your program:
java -cp . jp.co.worksap.global.Orienteering

adding JAR class path in UBUNTU

This is might be a common question but I am not able to add class path for a JAR file in UBUNTU. I have given below all the details I know:
java is located here:
the o/p of which java command is - /usr/bin/java
sudo vim /etc/bash.bashrc
export CLASSPATH=$CLASSPATH:/downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar
ps: downloads folder is directly under the root
sudo vim /etc/environment
CLASSPATH="/usr/lib/jvm/jdk1.7.0/lib: /downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar:"
As you can see, I have added the class path in bashrc and etc/environment... but still I am getting an error while trying to run the S3Sample.java which comes with awssdk for java.
when I compile the java file, I get the following errors:
ubuntu#domU-12-31-39-03-31-91:/downloads/aws-java-sdk-1.3.24/samples/AmazonS3$ javac S3Sample.java
S3Sample.java:25: error: package com.amazonaws does not exist
import com.amazonaws.AmazonClientException;
Now, I clearly understand that the JAR file is not added to the class path and so I am not getting the error. I've also tried javac with the class path option - but it does not work :(
PS: JAVA home is set correctly as other java programs work properly.
To set the classpath, it is in most cases better to use the the -cp or -classpath argument when calling javac and java. It gives you more flexibility to use different classpaths for different java applications.
With the -cp and -classpath arguments your classpath can contain multiple jars and multiple locations separated with a : (colon)
javac -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass.java
java -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass
The classpath entry in the example sets the classpath to contain the current working directory (.), and the two jar files A.jar and B.jar.
If you want to use the CLASSPATH environment variable you can do
export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass

Building Java package (javac to all files)

How to compile all files in directory to *.class files?
Well, this seems pretty obvious, so I may be missing something
javac *.java
(With appropriate library references etc.)
Or perhaps:
javac -d bin *.java
to javac create the right directory structure for the output.
Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)?
Yet another way using "find" on UNIX is described here:
http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html
The following two commands will compile all .java files contained within the directory ./src and its subdirectories:
find ./src -name *.java > sources_list.txt
javac -classpath "${CLASSPATH}" #sources_list.txt
First, find generates sources_list.txt, a file that contains the paths to the Java source files. Next, javac compiles all these sources using the syntax #sources_list.txt.
Here's a code fragment that I use to build an entire project where, as usual, source files are in a deeply nested hierarchy and there are many .jar files that must go into the classpath (requires UNIX utilities):
CLASSPATH=
for x in $(find | grep jar$); do CLASSPATH="$CLASSPATH:$x"; done
SRC=$(find | grep java$)
javac -cp "$CLASSPATH" $SRC

Categories

Resources