Java/VScode- .Class Files Not Being Sent to Output Directory - java

I'm using VSCode for my IDE for Java. I have the Setting.json file configured to output my .class files to a directory called 'build':
"java.project.outputPath": "build"
My Project Folder/File Layout is as follows:
math
\src
Main.java
\lib
Math.java
\build <- This is where I want my .class files to be built keeping the same file structure as
in the src folder.
When I use
javac src\Main.java
The compiler writes the .class files in the .\src directory, but I'm telling it to write the output in the .\build directory. Any ideas what's going on?

I didn't see settings.json in the workspace directory structure you provided. I think this may be the cause of the problem.
"java.project.outputPath"
This setting only takes effect in the workspace scope.
You can create a folder named .vscode in your workspace, then create settings.json:
Add the codes in workspace settings.json: "java.project.outputPath": "build".

I fixed the problem. I was using the Command Line. VSCode only builds to the specified build path if the build command is run through the gui.

Related

Java jar file not working when move to another location

Ok so... here are my steps.
I have a folder on desktop. Lets called it DesktopFolder. Inside desktop folder, i have 2 folders. One called libs and one called src. Inside libs are two jar dependencies. Lets call them jar1 and jar2. Inside src, i have a java file. lets call it MyProgram.java
so I compile them in cmd with
javac -cp .;../libs/jar1;../libs/jar2; MyProgram.java
that compiles.
now I create a manifest.txt inside my src folder with the following:
Main-Class: MyProgram
Class-Path: ../libs/jar1 ../libs/jar2
<a new line>
then in my cmd, I navigate to my src directory and do:
jar -cvfm MyProgramJar.jar manifest.txt ./../libs MyProgram.class
this compiles a jar file called MyProgramJar
this is found inside my src (because i navigated my cmd directory to src)
When I run the executable, it works.
But when i move the MyProgramJar outside to desktop
it says it cannot find library. Why is that? How can I fix it?
The error itself is the JNI error.
hmmmm. thanks to #MadProgrammer (from the comment section). I learned that a jar file cannot access another jar file from within. The classpath you put into manifest is all relative classpath. Once you move the jar file outside, it wont be able to locate the file that your program depends on. There are ways around it like "fat" jars as mentioned by Madprogrammer that allows a jar to access another jar from within.
Another way is opening up dependencies jar and simply moving the files out of its own jar. That way, your code can reference those libraries directly.
A personal friend of mine used eclipse to build the jar. That seem to work even if you have a jar within a jar and you move the jar around. I'm assuming eclipse did something just like "fat" jar.

Finding .class file in Eclipse

I need to turn in the .class file of a class from Eclipse. I managed to turn in the .java version of the file by clicking and dragging it into my browser, but I'm not sure where to find a .class file. I've read that I can find it in the "bin" but I have no idea where this is.
Thanks for all the help!
Generally, Eclipse projects have the following file system:
Proj/
.classpath
.project
src/
Foo.java
bin/
Foo.class
So, navigate to the Project folder, go to bin, and your classes should be right there.
If you are on a mac:
If you are in your project in the command line (you have done cd project)
You can do
cd bin
open .
And your class files should show up in a place that you can drag them into a browser. I don't understand why you need to submit .class files, as they are generated when the code is compiled.
Ctrl + Shift + T
will list down all the files including .class files in all of your projects in the workspace.

Export generated class files and resources equivalent command line command

I'am working on a python script , which involves few steps of creating a jar file.
However when i run the following lines of code i get the jar file created, having .java files and not .class files .
subprocess.call(['jar', 'cvf', 'process.jar','C:/my data/temp/process/src'])
Is there any way to get the jar file created with .class files similar to Export generated class files and resources checkbox in export jar dialog in eclipse .
Thanks
Make sure where is your *.class files located, I assume it is in bin directory or something:
subprocess.call(['jar', 'cvf', 'process.jar','C:/my data/temp/process/bin'])

Building executable jar file from .class files in eclipse

I am trying to build the project Sharpen by versant. I will start out that I don't know anything really about java and it's tools, hence why I am trying to build Sharpen(Java to C# converter). So I ended up building the project, but now I am left with a bunch of .class files in the bin directories.
How can I turn those .class files into a executable .jar file so I can run it? Sharpen is an eclipse plug in, so will I need to do anything extra, and will I have to manually remove files from their subdirectories in the bin folder and add them to the main bin directory?
Go to file->export->JAR file,
select "Export generated class files and sources" and make sure that your project is selected, and all folder

Tool for creating jar files from command line

I'm not very skilled in writing batch files and/ or java. I have a folder with several .class-Files and folders in it and I want to put them all into a executable .jar-File. I know that there is a tool called "jar - The Java Archive Tool", but that just won't work for me. My folder structure looks like this:
Folder "test"
File (a.class)
Folder "subdirectory"
File (b.class)
I want a executable .jar-File called file.jar. In this .jar should now be the file a.class and the folder subdirectory with the file b.class in it.
I don't get the .jar-Tool to run and the 7zip command line doesn't support .jars (I can't even add files to it). I want this to run from a .bat-File, so I just have to open the batch-file, it creates the .jar and puts the files in it and then closes itself again.
I hope you can help me and get what I mean.
If you have issues in executing jar command, then probably you would need to check if your path has been set correctly. You can verify if the path contains jdk location ( echo %path%) from command prompt. If not you need to update. Also you can verify using Javac -verbose command to see jdk is installed.
Once you have jdk path set, you can try below command to create jar
Jar -cf myapp.jar * --> includes all files including files from sub folders.
If you want to run from batch, you would need to mention path before jar command. Ideal place for path is to configure as environment property.
Create a text file (and call it something useful like manifest.txt). In it, put the line:
Main-Class: a
(which should be called A by convention) and include a hard return at the end.
Then create the jar with:
jar cvfm file.jar manifest.txt *.class
or
jar cvfm c:\somedir\file.jar c:\somedir\mainfest.txt *
You could just put that line in a batch file called createJar.bat and run it if you only want to create files called 'file.jar'
hth

Categories

Resources