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

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

Related

No Resources are loaded when compiling manually with javac

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

Ini4j classpath no class

i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp

Run java program sing Cmd

I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.

How to run Java program in command prompt

I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments

Problems compiling and running Java app with Bluecove (NoClassDefFoundError)

I have this app that uses bluetooth, so I need both, bluecove and bluecove-gpl packages, when I run it in NetBeans I have no problem at all, and works perfectly fine. But I still can't compile and run from the command line (Ubuntu 11.04).
I'm using this line for compilation:
$ javac -Xlint:unchecked -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client.java
And it doesn't return errors and it generates a .class file
Then I try to run the .class file like this:
java -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
But it returns a NoClassDefFoundError.
Could not find the main class: SPPClient.
Why is this happening?
You probably need to add your current directory (or whatever directory your class files reside in) to the class path.
Try something like
java -classpath .:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
or
java -classpath bin:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
You need to have the main class definition in the manifest file:
Main-Class: classname

Categories

Resources