0

For my intro computer science class they recommend using eclipse as an IDE. I have used vim in the past and would prefer using it. There are two .jar files that the programs we create rely off of, because it is an intro class and we are not using java's main class functionality.

Right now we download the two .jar files and then use the Eclipse IDE build path function to link the .jar files with our code. Then when we run on Eclipse IDE it works perfectly fine.

How would I do this in ubuntu terminal? Thank you!

TLDR; Intro comp sci class wants us to use eclipse, I want to use vim. How do you build path for a jar file to work with my class code in the ubuntu terminal.

Looked at this link and did not work Java: how to import a jar file from command line

Update of imageenter image description here

Community
  • 1
  • 1
  • Possible duplicate of [Java: how to import a jar file from command line](http://stackoverflow.com/questions/945962/java-how-to-import-a-jar-file-from-command-line) – Arnaud Jan 10 '17 at 15:48
  • Looked at that, didn't work –  Jan 10 '17 at 15:52
  • I know you love vim, but until you get it over that, and start using an IDE you will not be a productive java developer, or at least not nearly as productive as you could be. – MeBigFatGuy Jan 10 '17 at 15:53
  • @MeBigFatGuy Thank you for that! I will start testing out the IDE once I get vim working –  Jan 10 '17 at 15:55
  • @CtrlAltDelete : the path of your class must match its package : `com/mypackage/lab1.class`. Also add `.` to the classpath, it is the current directory . – Arnaud Jan 10 '17 at 15:56

2 Answers2

2

You specify the jar files with a CLASSPATH, either using a CLASSPATH environment variable

export CLASSPATH="a.jar:b.jar"
java com.mypackage.MyClass

or on the command line with -cp like

java -cp a.jar:b.jar com.mypackage.MyClass
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Are you sure the package you are mentioning in the command line is the correct one? – suvartheec Jan 10 '17 at 16:00
  • Yes, there are two .jar files one is for testing one is for images. We are not creating any images this lab so I only need the one .jar file –  Jan 10 '17 at 16:01
  • remove the colon sign after the jar. Its the separator when multiple ones are to be added – suvartheec Jan 10 '17 at 16:05
  • Still says: `Could not find or load main class com.mypackage.lab1` –  Jan 10 '17 at 16:08
  • and what i meant with my previous comment was that are you sure `com.mypackage.lab1` is the correct package for your class – suvartheec Jan 10 '17 at 16:09
  • Where it says ".mypackage" am I supposed to enter the name of a package? If so that is the problem, I never created a package. Is that is what is wrong? –  Jan 10 '17 at 16:11
  • Yes when people write mypackage/myclass they mean **your** package or class. – suvartheec Jan 10 '17 at 16:16
0

Include your jar dependencies while executing from command line.

java -cp tester.jar lab1
suvartheec
  • 3,484
  • 1
  • 17
  • 21
  • This doesn't work `Error: Could not find or load main class lab1` –  Jan 10 '17 at 16:21
  • Do I need to make a package before doing this –  Jan 10 '17 at 16:22
  • Are you sure your lab1 class includes the main method in it? And no you dont compulsorily need a package. but to start off you are better without it. Please consider using an IDE like Eclipse to resolve the basic mistakes easily. – suvartheec Jan 10 '17 at 16:25
  • The lab1 class does not have a main method in it, I mentioned that above. I will use an IDE but I want to figure out how it works before I use it. I like knowing how things work before I use them –  Jan 10 '17 at 18:41
  • Then please take a look at the numerous Java tutorials about how Java programs work before you start writing code! https://www.tutorialspoint.com/java/java_quick_guide.htm is one of the thousands available. The `main` method is the entry point of the Java class which the JVM seeks and starts executing. Without it, it cant get in and start execution, hence the runtime error – suvartheec Jan 11 '17 at 09:24