I am compiling java file with command prompt with some package name come.test.
But while running with java command it gives no class definition error. If I compile with IDE it is running because folders are created like com\test
How can I get those folders if I compile with javac command.
For such questions, when an online search at oracle does yield immediate results, try
javac -help
javac -source 1.8
-target 1.8
-encoding UTF-8
javac -d target/classes --> generated classes
-s src/main/java --> generated source files
src/main/java/x/y/*.java --> java files to be compiled
The directory target/classes should exist. Package folders are created under classes (x/y).
Related
How to use the javac command, want to compile hello.java to hello.class, the file content is as follows:
How to introduce other packages, the directory structure is as follows
It's a mistake for me to run like this:
e:\icenew2\hellotest>javac -sourcepath src hello.java
It's wrong to run like this : javac -cp "src\com\zeroc\Ice*.class" hello.java
We can use the wildcard character *:
javac -cp *; BackBenchers.java
Specifying destination directory:
javac -d classes backbenchers.java
Note: The compiler will complain if the specified directory does not exist, and it won’t create one.
If the source file is under a package, the compiler will create package structure in the destination directory.
Specifying source path directory:
javac -sourcepath src BackBenchers.java
I have multiple .java files inside a folder (e.g temp/code/project)
I want a batch file that will compile and run these java files.
The batch file should create class files inside the same structure where java files are located starting with the classes folder (i.e classes/temp/code/project)
I don't know how to write a batch file. Can any body help me with this? Thanks in advance.
Theoretically you should write the following:
#echo off
::compile classes
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass1.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass2.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass3.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClassLauncher.java
:: create jar
jar cvfM Manifest.txt myjar.jar *.class
echo.
echo Hit any key to launch project.
pause
java -jar myjar.jar
pause
If you have one class that depends on all other classes in your project it is enough to run javac with this class only: compiler will compile everything.
# Manifest.txt
Manifest-Version: 1.0
Class-Path: .;MyUtils.jar
Created-By: 1.6.0
Main-Class: com.yourcompany.YourClassLauncher
This is only example and a good exercise. In real life people use special build tools like good old Ant, Maven or newer Graidle or Buildr. I'd recommend you to take one of them.
Something along the lines of
cd C:\temp\code\project
javac -classpath . -d C:\classes\temp\code\project\ *.java
You may not need to include the -classpath tag (I always do so that I don't have to worry about maintaining my CLASSPATH variable. The -d tag specifies a particular directory to place generated class files.
I have the .java file on the current working directory but javac reports:
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
I'm working on ubuntu.
From your comment above, it looks like you tried:
javac -cp .;lib.jar a.java on your Ubuntu system. The CLASSPATH separator is : on Unix systems and ; on Windows.
Ubuntu considered the command up to the ;, java -cp . and thus gave the message.
javac -cp .:lib.jar a.java should compile fine.
For anyone who is using powersehll on windows use CLASSPATH separator : instead of ;
I tried a similar thing and found that you need to mention the absolute path when you are using the
-cp and -d option with javac like this
javac -cp 'ur location of jars & files'; -d 'location to add your classes to' 'absolute path of file'
eg:
javac -cp C:\home\lib\mywork; -d c:\home\classes c:\home\files*.java
for javac, there are options and arguments
arg: it takes argument as path of source file
options: we require for basic compilation
-sourcepath: the path of dependent source files
-d: directory path of output classes
javac -sourcepath './src' -d './bin' -verbose './src/App.java'
I'm writing a makefile that compiles a .java file in a different directory, and then I want to run it, without changing directories. I want to do something along the lines of:
$(SQM_JAVA_TOOL_DONE) : $(SQM_JAVA_TOOL)
$(shell cd /home_dir)
javac myjavafile.java
java myjavafile
where the Java file is /home/myjavafile.java, and the makefile isn't running from /home.
How can I do this?
I might be misunderstanding the question, but you can compile with
javac /home/MyJavaFile.java
This will create MyJavaFile.class in /home
You can then run it by including /home on the classpath. e.g.
java -cp /home MyJavaFile
If you want to generate the class file in a different directory then you can use the -d option to javac.
Use the -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. Then, to run the program, simply include this directory in the classpath:
javac -d some/directory myjavafile.java
java -cp some/directory myjavafile
Just to add to the existing answers, you may want the --source-path flag:
--source-path <path>, -sourcepath <path>
Specify where to find input source files
I believe this effectively sets the package root javac will compile from (i.e. <path> will be stripped from the expected package name of the files). It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, not the path passed to --source-path.
For example, to compile and run from a project's root where source is stored in src/ and you want it build in bin/:
$ javac --source-path src -d bin src/mypackage/*.java
$ java -cp bin mypackage.Main
This works even from directories elsewhere in the filesystem, e.g.:
$ javac --source-path /some/absolute/path/src -d /some/absolute/path/bin /some/absolute/path/
$ java -cp /some/absolute/path/bin mypackage.Main
I am using VS Code and installed java and code runner extensions. When I created new java project using the extension, it was creating the .class file in src instead of bin. To solve the issue I opened settings.json file from File > Preferences > Settings and searched for "settings" (or "code-runner"). Then I added following lines in that file.
"code-runner.executorMap": {
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && java -cp bin $fileNameWithoutExt",
}
If you don`t want to see the command that runs before code file then add these lines instead:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.executorMap": {
"java": "there is && clear added in the execution paramater"
"java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && clear && java -cp bin $fileNameWithoutExt",
}
I hope this finds someone with similar issue.
Really basic question I'm sure for some of you Java heads out there.
I have a list of java files and jars that are required.
On windows to build I have this batch file
javac -cp .;opencsv-1.8.jar;mysql-connector.jar -source 1.4 -target 1.4 *.java
jar cvf cup.jar *.class
del *.class
If I want to do the same thing on mac how would a write a shell script to do the same?
Basically the same thing, except
The path separator is ':' instead of ';'
I believe the command to delete is called 'rm'
Also, I'd put a shabang at the start.
So:
#!/bin/sh
javac -cp .:opencsv-1.8.jar:mysql-connector.jar -source 1.4 -target 1.4 *.java
jar cvf cup.jar *.class
rm *.class