create java class file in another directory - java

say ~ is my home directory. it got a child directory java , which has two child directories classes and sources. Say I got a file in ~/java/sources/ names Graph.java. I want to compile it such that its class file ends up being in ~/java/classes/.

javac Graph.java -d "~/java/classes/"

Use -d parameter with javac to specify the directory you'd like to store the compiled class file.
As Ex:
javac -d <path where to store class file> <your java file to compile>

Related

how to run a java class in another folder with jar file

i'd like to run a java class on other folder, i have a mysqlcon.jar on the current path and a PetsGUI.class on ./classes/
when i try to run it by doing
java -cp .:mysqlcon.jar -d classes/PetsGUI
i receive
classes/PetsGUI not found
if i move mysqlcon.jar on classes and type
java -cp .:mysqlcon.jar PetsGUI
on classes/ it runs, so the code is correct. what's the correct command to run it?
So you need the jar file, and the classes directory in the classpath:
java -cp ./classes:mysqlcon.jar PetsGUI
java doesn't expect a file path as argument. It expects the fully quelified name of a class. And that class is then searched on the classpath.

Use javah to generate header file

i am triyng to generate header file for native use (c/c++) from a java file using eclipse.
from the command line i can only reach the javah when i am in this location :
C:\Program Files (x86)\Java\jdk1.7.0_51\bin>
but i cant reference my java class like so :
C:\Program Files (x86)\Java\jdk1.7.0_51\bin>javah -jni com.or.jnihelloworld.nativeclass
because the class located outside of this folder at :
C:\Users\Or Azran\workspace\JniHelloWorld\src\NativeLib.java
and i want to make this file in to a jni folder in :
C:\Users\Or Azran\workspace\JniHelloWorld\jni
how can i do it from command line?
a good toturial will be also great
There appears to be a couple of issues to solve here.
First. I'm not sure that your source code is set up correctly. If indeed your class is com.or.jnihelloworld.nativeclass then it should be in directory: C:\Users\Or Azran\workspace\JniHelloWorld\src\com\or\jnihelloworld\nativeclass.java
However, assuming that the class/directory is correct. The javah command uses -d to specify the output directory, and you can specify the path with -classpath so
javah -classpath "C:\Users\Or Azran\workspace\JniHelloWorld\src\" \
-d "C:\Users\Or Azran\workspace\JniHelloWorld\jni" com.or.jnihelloworld.nativeclass
should put the file where you want it.
Your PATH doesn't include the bin directory of the JDK.

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

Generate JNI header file in different packages

If I have all my .java and .class files in one place (i.e. in the default package) then everything is OK and I do all the JNI stuff, etc.
But in this case I have package-ception (lots of directories), my class and Java files are separated in /bin and /src and so on. And I need to generate the header file, but I am getting errors all the time. I tried so many commands, I saw different tutorials. I am already out of options.
So my project is in c://gvk/SEP3 and then the class and Java files with the native methods that I am gonna use are in /bin/CalculatorServer and /src/CalculatorServer
I have all the time run the javah command from the directory where the class file with the native methods is. The commands I tried so far are:
javah -d ./CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -d ./CalculatorServer CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'CalculatorServer.NativeMethodsCalculator'.
javah -d c://gvk/SEP3/bin/CalculatorServer -classpath c://gvk/SEP3/bin/CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -classpath c://gvk/SEP3/bin/CalculatorServer -o NativeMethodsCalc.h src.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'src.CalculatorServer.NativeMethodsCalculator'.
javah -jni bin.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'bin.CalculatorServer.NativeMethodsCalculator'.
What you didn't try: go just to /bin/ (not into CalculatorServer) and run
javah -jni CalculatorServer.NativeMethodsCalculator
This is the only way how to run it. Just look at the javah doc. It says "fully-qualified-classname" in the synopsis. "Fully qualified" means full classpath. You were giving it only the classname. It worked for you so far only because you were using a default package, which means that your fully qualified classname was equal to a bare classname.
Option -d and -o doesn't influence the class lookup, only the storage of native result. All the variants you tried do not make any difference to your mistake.
I have all the time run the javah command from the directory where the class file with the native methods is
That's your mistake. You should run it from the directory that contains the outermost package, with the inner packages and their .class files below it. Then you don't need a -d argument or a -classpath argument. Assuming your outermost package is CalculatorServer, you should be in the directory containing CalculatorServer, and the command line required is javah CalculatorServer.NativeMethodsCalculator.

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...

Categories

Resources