How can I compile terminal programs which uses external libraries? I'm using JColor (https://github.com/dialex/JColor) to color my font but I don't know how to compile it.
My IDE is IntelliJ. I already tried to compile my program into a jar but executing it in terminal ignores JColor. No error but no color and cryptic symbols instead. I don't know if it's cause of JColor or I missed something during compiling.
Thank you very much in advance.
Edit : The real issue
(See replies)
We found the issue not to be related to imports, but rather that ANSI control support is disabled within windows terminals by default, but enabled within IntelliJ.
This made it seem as though the library was not working after being exported, or wasn't being exported.
Relavent Discussion
The dependancy
InteliJ gives the option to export libraries with the module from within the Project Structure window:
Assuming the library is configured within your project, all you have to do is navigate to Project Structure > Modules > myModule > Dependencieswhere you can add the library, and tick export
The artifact
Next, create an artifact from your module, with depenencies
The output layout shows the internal layout of the jar after export, check the library is in there.
Now when you build your artifact, it should contain the dependency.
javac -cp /path/to/jar/file Design.java
java -cp .:/path/to/jar/file Design
So, lets suppose your current working directory in terminal is rc/structure/
Related
I'm using the Java Test Runner extension in VScode, and I want it to compile my classes to a specific folder. I found the setting to change the -cp when actually running the compiled .class files, but the whole thing still fails since the extension builds the .class files in an obscure temporary folder deep in it's appdata folder.
I would like it to compile my .java classes into a folder I have within my project, so that I can have them there. I have tried googling the sh*t out of this and either I have no idea what I'm saying or noone has ever had a problem with this before.
Thanks in advance for the help.
This is a late answer, but I just ran into this so I hope it helps others.
As of Feb 4, 2021 there is now a setting for the Java Language Support for VS Code extension, where you can specify the output directory for compiled .class files. If you're using VS Code to run Java I'm almost certain you'd have this extension installed.
Simply open your VS Code settings and search for java.project.outputPath. As you'll notice, it states this setting only applies for Workspace settings, not User settings. So make sure to switch to the Workspace tab before clicking Edit in settings.json. This will edit the settings.json file in your projects .vscode folder.
Also note this doesn't work for projects managed with Maven/Gradle. It's great for simple projects though.
For the curious: link to the GitHub pull request where the feature was added to the extension (also has a neat gif of feature in action): https://github.com/redhat-developer/vscode-java/pull/1694
https://blog.usejournal.com/visual-studio-code-for-java-the-ultimate-guide-2019-8de7d2b59902
Under "Adding Libraries and JARs"
First of all,
you can create a CLASSPATH location in your environment variable section, after you set your path location.
Also, what people usually mistake is that they install a code runner used to compile and run all the languages, so if you are running your java code through the top-right run button,
then click on the drop down menu beside it and click the "run java" button and I guess then your code will run just fine.(if you have downloaded java runner extension separately)
I hope this helps!
When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.
I'm currently working on a project where I'm using the LWJGL library and I just tried building a .jar out of it all, now the problem is that nothing happens when I try to start the .jar.
My only thought is that maybe the library didn't export correctly but I've been looking around at the other questions here for a while and I've have added everything correctly at the Dependencies tab in the Project Structure, the libraries I need are also in the Artifact Output Layout as extracted directories.
I just tried checking the stacktrace with cmd and it says that it can't find lwjgl in the library path so I'm guessing there's something I don't know about .jar files or something that's causing this error.
Here's the stacktrace:
Also in IntelliJ I've put "-Djava.library.path=lib\native" in VM options, is it possible that this doesn't apply to the .jar or something.
Help is appreciated!
There is a rather complicated process to make an executable jar with LWJGL. You need to use Jarsplice.
Jarsplice has 4 steps which are pretty self explanatory but here they are anyway:
1: Add Jars.
Add the jar you generated as well as all the libraries. That includes lwjgl.jar, and any other jars you used.
2: Add Natives.
Add all the natives you used in your project. These are the files you referenced using -Djava.library.path.
3: Main Class
Select the main class of your program. This could be something like com.example.game.EntryPoint.
4: Create Fat Jar
Click "Create Fat Jar" to create your executable jar!
You can also optionally create a Windows .exe, OSX .app, and Linux .sh executables as well.
I'd like to build my program in NetBeans so that the released executable includes all dependencies required to run it (for example: it needs cygwin1.dll). I am currently not able to run my executable on another machine without having cygwin1.dll inside the same directory. I don't mind if the executable is a large file.
Is there a setting in NetBeans to automatically detect/allow that? Do I need to set/turn on "static"?
I seems that cygwin1.dll cannot be statically linked.
Reference:
Can you statically compile a cygwin application?
first of all sorry for my bad english. I have a question and I cant find an answer anywhere.
My Programm exports some loose *.java files withouth any entrypoint. The written API allows an CMD to be executed. Problem is, the CMD Order to start eclipse with parameters. My questions is is it possible to start eclipse 3.3-3.6 with a command line parameter to create a project. Creating a Workspace through -data c:\xxxx\myworkspace is no problem but since eclipse sees no files outside a project, i need to make one so i can put my java files into it and startup an remote debug after it. The Plan is to startup an external export java file, which starts eclipse with the defined workspace and a fresh project (with the exported java files) for usage.
Using Maven it's possible to create an Eclipse project and all kinds of useful stuff, http://maven.apache.org/guides/mini/guide-ide-eclipse.html gives at least a starting point
There are some tools like Maven or Ant plugins that create Eclipse projects from some structural information (e.g. the Maven plugin reads the pom.xml).
If your code is unstructured it might be hard to create a fully functional Eclipse project. You might be able to create a Java project and define its source path (in fact it's a simple XML file called .project - you could easily write one yourself) but any other information like additional dependencies etc. would have to be added manually.
Besides that, creating a simple project from existing sources isn't that hard in Eclipse. Why don't you just do it manually?